Support

Account

Home Forums ACF PRO Add field programmatically to group

Solved

Add field programmatically to group

  • Hi guys,

    I’ve been using ACF Pro for a while now and it is quite useful!
    Right now I’m trying to create an add-on for my plugin that needs to add some fields to a flexible content field.
    I can’t seem to find any documentation on it though.
    Since I want to keep it open for the users, it should still be possible for them to adjust these groups. So I’m not looking for the local fields solution.

    Does anyone have any idea how to fix this?

    Thanks in advance!
    Tim

  • Hi @timm

    I think you can use the Local JSON feature and then let your users sync the field groups. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/synchronized-json/.

    But please keep in mind if you allow your user to modify the field groups, it can cause issues in the future because they can change anything, even if they don’t know the usage of the changed options.

    I hope this makes sense 🙂

  • Hi @James

    Thanks for your help so far, but I don’t think this solves my question.
    I’d like to add fields to an already existing flexible content field.
    Is this also possible with synchronized json? If so, how can I implement it?

    Thanks for your help so far!

    Tim

  • Hi @timm

    The problem is that you said you want your users to be able to adjust the field groups. Please keep in mind that it’s not possible to modify the fields registered via PHP code from the backend.

    Also, please bear in mind that you can’t mix between the fields created from the backend and the one created via PHP code. This means that you can only add fields using PHP code to the flexible content you’ve created via PHP code too.

    If that’s what you are after, then you should be able to add a new field to a flexible content layout like this:

    acf_add_local_field(array(
        "key" => 'field_key',
        "label" => 'field label',
        'name' => 'field_name',
        'type' => 'text',
        'parent' => 'field_1234567890abc',
        'parent_layout' => 'abcdefghijklm',
    ));

    Where “field_1234567890abc” is the flexible content key and “abcdefghijklm” is the layout key.

    I hope this makes sense 🙂

  • Hi @james,

    Isn’t it weird that it is not possible to post fields with php? Since this is exactly what happens when you load a json file with the import tool. I can’t seem to figure out how this works though..
    Any idea if this is usable?

    Sincerely,
    Tim

  • Hi @timm

    I think there’s a misunderstanding here. Could you please let me know what do you mean by “post fields with PHP?”

    Also, could you please explain your goal again in more detailed explanation?

    Please keep in mind that importing using JSON file will save the fields to your database while using PHP won’t do that. When certain page loads, ACF will try to get the field settings from both the database and PHP code and process it separately to avoid conflicts on the page.

    Thanks 🙂

  • Hi @james,

    With “post fields with PHP” I mean that when you import a json file, it puts those files in a database right? If that is possible, shouldn’t it also be possible to add new fields programmatically? If they are not in a database, where are they stored?

    Thanks a lot!
    Tim

  • Hi @timm

    Thanks for the explanation.

    The field group registered via PHP is saved in the PHP file. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/register-fields-via-php/.

    So when the page load, ACF will check if there’s a field registered in the PHP file. If they are, ACF will show it accordingly.

    But I think I know your goal now. I believe you want to add/modify a field group to the database, but via PHP code, right? In this case, you need to check the source code to see how ACF adds a new field. You can check the acf_update_field_group() function, which is located in “/wp-content/plugins/advanced-custom-fields-pro/api/api-field-group.php”.

    I hope this helps 🙂

  • I’ve already checked the source code, but I’ve not been able to find this.
    So for now I made the step to the given link above, but I can’t seem to get this to work with acf_form(). Is there something I need to add to the options?
    I pasted the following in functions.php:

    acf_add_local_field_group(array (
    			'key' => 'group_1',
    			'title' => 'My Group',
    			'fields' => array (
    				array (
    					'key' => 'field_1',
    					'label' => 'Sub Title',
    					'name' => 'sub_title',
    					'type' => 'text',
    					'prefix' => '',
    					'instructions' => '',
    					'required' => 0,
    					'conditional_logic' => 0,
    					'wrapper' => array (
    						'width' => '',
    						'class' => '',
    						'id' => '',
    					),
    					'default_value' => '',
    					'placeholder' => '',
    					'prepend' => '',
    					'append' => '',
    					'maxlength' => '',
    					'readonly' => 0,
    					'disabled' => 0,
    				)
    			),
    			'location' => array (
    				array (
    					array (
    						'param' => 'post_type',
    						'operator' => '==',
    						'value' => 'book',
    					),
    				),
    			),
    			'menu_order' => 0,
    			'position' => 'normal',
    			'style' => 'default',
    			'label_placement' => 'top',
    			'instruction_placement' => 'label',
    			'hide_on_screen' => '',
    		));

    book is a post type that I created.
    And this is what I use to create the form:

    $options = array(
    
        	/* (int|string) The post ID to load data from and save data to. Defaults to the current post ID.
        	Can also be set to 'new_post' to create a new post on submit */
        	'post_id' => 'new_post',
        	'form_attributes' => array( // attributes will be added to the form element
            'class' => ''
          ),
    
        	/* (array) An array of post data used to create a post. See wp_insert_post for available parameters.
        	The above 'post_id' setting must contain a value of 'new_post' */
        	'new_post' =>  array(
        						'post_type'		=> $_POST['type'],
        						'post_status'		=> $post_status
        					),
    
        	/* (boolean) Whether or not to show the post title text field. Defaults to false */
        	'post_title' => true,
    
        	/* (boolean) Whether or not to show the post content editor field. Defaults to false */
        	'post_content' => true,
    
        	/* (boolean) Whether or not to create a form element. Useful when a adding to an existing form. Defaults to true */
          	'form' => false
          );
        acf_form($options);

    Thanks for your help so far!

    Tim

  • Hi @timm

    I believe that’s because the new_post attributes have the wrong values. Could you please debug them like the following?

    echo "POST['type']:\n";
    var_dump($_POST['type']);
    
    echo "\npost_status:\n";
    var_dump($post_status);

    Also, could you please set them manually like this:

    'new_post' =>  array(
        'post_type'	=> 'book',
        'post_status' => 'published'
    ),

    Thanks 🙂

  • Hi @james,

    Although that could have been the problem, the post type is correct since I do get an ACF Form. The problem is that the local fields are missing. Is there something that should be added to the options to combine the local and the database fields in an acf_form?

    Thanks

    Tim

  • Hi @timm

    Could you please let me know the ACF version you are using?

    I’ve tested it on my installation but failed to reproduce the issue. Could you please try to reproduce the issue on one of the WordPress’ stock themes (like Twenty Sixteen) with other plugins deactivated? If it disappears, then you can activate the theme and plugins one by one to see which one causes the issue.

    Also, could you try to pass the field groups key to the “field_groups” option like this:

    'field_groups' => array('group_1'),

    Thanks 🙂

  • Thanks for your help @james!

    By adding the field_group it worked perfectly!
    Since I thought this was weird, I’ve double checked the post type that I gave the local group. Apparently I made a typo in there, which is why it didn’t show in the beginning.

    Thanks a lot for your help!

    Tim

Viewing 13 posts - 1 through 13 (of 13 total)

The topic ‘Add field programmatically to group’ is closed to new replies.