Support

Account

Home Forums ACF PRO Create new field group

Solving

Create new field group

  • I want o create new field group People. I am using the following code but in the back-end nothing created.

    
    f ( function_exists( 'acf_add_local_field_group' ) ) {
    
    		// ACF Group: People
    		acf_add_local_field_group( array (
    			'key'      => 'group_person_details',
    			'title'    => 'Person Details',
    			'location' => array (
    				array (
    					array (
    						'param'    => 'post_type',
    						'operator' => '==',
    						'value'    => 'person',
    					),
    				),
    			),
    			'menu_order'            => 0,
    			'position'              => 'normal',
    			'style'                 => 'default',
    			'label_placement'       => 'top',
    			'instruction_placement' => 'label',
    			'hide_on_screen'        => '',
    		) );
    
    		// First name
    		acf_add_local_field( array(
    			'key'          => 'field_first_name',
    			'label'        => 'First Name',
    			'name'         => 'first_name',
    			'type'         => 'text',
    			'parent'       => 'group_person_details',
    			'instructions' => '',
    			'required'     => 1,
    		) );
    
    		// Middle name
    		acf_add_local_field( array(
    			'key'          => 'field_middle_name',
    			'label'        => 'Middle Name',
    			'name'         => 'middle_name',
    			'type'         => 'text',
    			'parent'       => 'group_person_details',
    			'instructions' => '',
    			'required'     => 0,
    		) );
    
    		// Last name
    		acf_add_local_field( array(
    			'key'          => 'field_last_name',
    			'label'        => 'Last Name',
    			'name'         => 'last_name',
    			'type'         => 'text',
    			'parent'       => 'group_person_details',
    			'instructions' => '',
    			'required'     => 1,
    		) );
    
    		// Image
    		acf_add_local_field( array(
    			'key'           => 'field_image',
    			'label'         => 'Image',
    			'name'          => 'image',
    			'type'          => 'image',
    			'parent'        => 'group_person_details',
    			'instructions'  => '',
    			'required'      => 0,
    			'return_format' => 'array',
    			'preview_size'  => 'thumbnail',
    			'library'       => 'all',
    			'min_width'     => 0,
    			'min_height'    => 0,
    			'max_width'     => 0,
    			'max_height'    => 0,
    		) );
    
    		// Office
    		acf_add_local_field( array(
    			'key'          => 'field_office',
    			'label'        => 'Office',
    			'name'         => 'office',
    			'type'         => 'text',
    			'parent'       => 'group_person_details',
    			'instructions' => '',
    			'required'     => 0,
    		) );
    
    		// E-mail
    		acf_add_local_field( array(
    			'key'          => 'field_email',
    			'label'        => 'E-mail',
    			'name'         => 'email',
    			'type'         => 'email',
    			'parent'       => 'group_person_details',
    			'instructions' => '',
    			'required'     => 0,
    		) );
    
    		// Office Phone
    		acf_add_local_field( array(
    			'key'          => 'field_phone',
    			'label'        => 'Phone',
    			'name'         => 'phone',
    			'type'         => 'text',
    			'parent'       => 'group_person_details',
    			'instructions' => '',
    			'required'     => 0,
    		) );
    
    		// Affiliation
    		acf_add_local_field( array(
    			'key'             => 'field_affiliation',
    			'label'           => 'Affiliation',
    			'name'            => 'affiliation',
    			'type'            => 'taxonomy',
    			'parent'          => 'group_person_details',
    			'instructions'    => '',
    			'required'        => 0,
    			'taxonomy'        => 'affiliation',
    			'field_type'      => 'multi_select',      // UI (checkbox, multi-select, select, radio)
    			'allow_null'      => 0,             // Can select a blank value
    			'load_save_terms' => 1,             // Persist using term relationships table
    			'return_format'   => 'object',      // or 'object'
    			'add_term'        => 0,             // Can the user add new terms?
    		) );
    	}
    }
    
    add_action( 'acf/init', 'register_custom_acf_fields' );
    
  • You are adding this action, but I do not see the function that is supposed to run

    
    add_action( 'acf/init', 'register_custom_acf_fields' );
    

    Adding an action requires a corresponding function

    
    function register_custom_acf_fields() {
      // your code to add field group here
    }
    
  • Sorry my mistake, i have added the action as you wrote but the result is the same. No acf created.

  • Where are you adding this code?

  • In functions.php file

  • Given the information you’ve provided I don’t see anything that would cause the field group not to appear.

    The first step is to see if your actions is actually running.

    At the top of your function register_custom_acf_fields() add something like

    
    die('the filter is running');
    

    and see if that causes loading to die and output the string.

  • I have added the die(‘the filter is running’); at the top of my function and is running.
    Is there any other way to create dynamically fields when i call a function?

  • I still do not see any reason why it’s not working. Are you sure you have the correct value for the post type in your location rules?

  • My mistake, i thought that acf will be created in custom fields group menu.

  • If you are running this on the acf/init hook, then I really cannot see any reason that this is not working.

    Try this

    
    if( function_exists('acf_add_local_field_group') ) {
      die('acf_add_local_field_group EXISTS');
    }
    
Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.