Home › Forums › ACF PRO › Add field programmatically to group › Reply To: Add field programmatically to group
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
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.