Home › Forums › Backend Issues (wp-admin) › Programmatically created fields doesn't save properly
Trying to add some fields programmatically to an option page, but when saving it applies the value set on the last iterated group to all previous iterations.
Can’t figure out why this is happening, some help would be appreciated!
Code below is setup in a mu-plugin
<?php
// Add Options Page
add_action( 'plugins_loaded', function () {
if( function_exists('acf_add_options_page') ) {
acf_add_options_sub_page(array(
'page_title' => 'Options',
'menu_title' => 'Options',
'menu_slug' => 'project-options',
'parent_slug' => 'edit.php?post_type=project',
));
}
});
// Setup fields for Options Page
add_action( 'init', function () {
if( function_exists('acf_add_local_field_group') ) {
$terms = get_terms( array(
'taxonomy' => 'department',
'hide_empty' => false,
));
foreach ($terms as $term) {
$department_name = $term->name;
$department_slug = $term->slug;
acf_add_local_field_group(array(
'key' => 'group' . $department_slug,
'title' => $department_name,
'fields' => array(
array(
'key' => 'field_' . $department_slug . '_layout',
'label' => 'Layout',
'name' => 'layout',
'type' => 'button_group',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array(
'full_width' => 'Full Width',
'grid' => 'Grid',
'grid_dynamic' => 'Dynamic Grid',
),
'allow_null' => 0,
'default_value' => '',
'layout' => 'horizontal',
'return_format' => 'value',
),
array(
'key' => 'field_' . $department_slug . '_order',
'label' => 'Order',
'name' => 'order',
'type' => 'button_group',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array(
'date' => 'By Date (Latest first)',
'manual' => 'By Custom Order',
),
'allow_null' => 0,
'default_value' => '',
'layout' => 'horizontal',
'return_format' => 'value',
),
array(
'key' => 'field_' . $department_slug . '_custom_order',
'label' => 'Custom Order',
'name' => 'custom_order',
'type' => 'relationship',
'instructions' => '',
'required' => 0,
'conditional_logic' => array(
array(
array(
'field' => 'field_' . $department_slug . '_order',
'operator' => '==',
'value' => 'manual',
),
),
),
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'post_type' => array(
0 => 'project',
),
'taxonomy' => array(
0 => 'department:' . $department_slug,
),
'filters' => array(
0 => 'search',
),
'elements' => array(
0 => 'featured_image',
),
'min' => '',
'max' => '',
'return_format' => 'object',
),
),
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'project-options',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'left',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
));
}
}
});
name
for each fields needs to be unique aswell. Now it works like it should 🙂
The topic ‘Programmatically created fields doesn't save properly’ is closed to new replies.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.