Support

Account

Home Forums ACF PRO Programatically adding field group and fields to the (or an) options page

Solved

Programatically adding field group and fields to the (or an) options page

  • Because I’ll be moving my theme from server to server, I want to set up all my field groups programatically. I’ve had no problem doing this in the past with custom post type field groups but I have been unable to get it to work for the standard options page. I initially set up the field group using the ui and then exported it as json and pasted that into my php. I also converted it back to a PHP array just in case that made a difference. Here’s my array:

    
    	$siteOptions = array (
    		array (
    		  'key' => 'group_5ca4e11ac450e',
    		  'title' => 'Site options',
    		  'fields' => 
    		  array (
    			array (
    			  'key' => 'field_5ca4e1333328c',
    			  'label' => 'Announcement',
    			  'name' => 'announcement',
    			  'type' => 'text',
    			  'instructions' => '',
    			  'required' => 0,
    			  'conditional_logic' => 0,
    			  'wrapper' => 
    			  array (
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			  ),
    			  'default_value' => '',
    			  'placeholder' => '',
    			  'prepend' => '',
    			  'append' => '',
    			  'maxlength' => '',
    			),
    		  ),
    		  'location' => 
    		  array (
    			array (
    			  array (
    				'param' => 'options_page',
    				'operator' => '==',
    				'value' => 'acf-options-site',
    			  ),
    			),
    		  ),
    		  'menu_order' => 0,
    		  'position' => 'normal',
    		  'style' => 'default',
    		  'label_placement' => 'top',
    		  'instruction_placement' => 'label',
    		  'hide_on_screen' => '',
    		  'active' => 1,
    		  'description' => '',
    		),
    	);	  
            register_options_page('Site');
            acf_add_local_field_group($options);
    

    The crucial part is the location element. This is the parameter that appears in the admin url and it is correct. This was a later attempt after I’d already failed to add the options to the native ACF options page using the value 'acf-options'.

    On the options page I just get an alert stating that no field groups have been created for this options page.

    I’ve not found any explicit instructions relating to the use of this function to set up an options field group, but likewise I’ve not found anything to say it shouldn’t work.

    Version in use is 5.8.0-beta4.1

  • I’m aware there’s an obvious bug there in using the incorrect variable name when passing to the acf_add_local_field_group function. That’s not the reason it’s not working, that’s just me copypasting from outside a wrapper function and then inside the wrapper function. I AM passing the correct array so ignore that.

  • Your field group is in a nested array

    
    $siteOptions = array(
      array(
        // field group settings
      )
    );
    

    remove the nesting

    
    $siteOptions = array(
      // field group settings
    )
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Programatically adding field group and fields to the (or an) options page’ is closed to new replies.