Home › Forums › Backend Issues (wp-admin) › acf local field group issue when saving page
acf local field group issue when saving page
Hello everyone !
I’m currently creating a plugin for a client. To create a plugin as isolated as i can, i decided to generate a custom wordpress page, forced in draft and menu_order -999. In this page, i want to generate ACF fields.
To generate the page, i’m doing this :
function GenerateCustomPage(){
global $option_page_title;
if (!get_page_by_path(sanitize_title($option_page_title))) { //If page doesn't exist
$page_args = array(
'post_title' => $page_title,
'post_name' => sanitize_title($option_page_title),
'post_content' => '',
'post_status' => 'draft',
'post_type' => 'page',
'menu_order' => -999,
'post_author' => 1,
);
wp_insert_post($page_args);
} else { //If the page exist, force draft and menu order
$page = get_page_by_path(sanitize_title($option_page_title));
$page->menu_order = -999;
$page->post_status = 'draft';
wp_update_post($page);
}
}
add_action('after_setup_theme', 'GenerateCustomPage');
To generate ACF fields, i’m usint the init hook. Such as :
function GenerateACFOptions() {
global $option_page_title;
if ( ! function_exists( 'acf_add_local_field_group' ) ) {
return;
}
acf_add_local_field_group( array(
'key' => 'group_floating_bar_options',
'title' => 'Floating Bar Options',
'fields' => array(
array(
'key' => 'field_floating_bar_item_1',
'label' => 'Item 1',
'name' => 'item_1',
'aria-label' => '',
'type' => 'group',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'layout' => 'table',
'sub_fields' => array(
array(
'key' => 'field_floating_bar_item_1_display',
'label' => 'Display',
'name' => 'item_display',
'aria-label' => '',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui_on_text' => '',
'ui_off_text' => '',
'ui' => 1,
),
array(
'key' => 'field_floating_bar_item_1_link',
'label' => 'Lien',
'name' => 'item_link',
'aria-label' => '',
'type' => 'link',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'return_format' => 'array',
),
array(
'key' => 'field_floating_bar_item_1_image',
'label' => 'image',
'name' => 'item_image',
'aria-label' => '',
'type' => 'image',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'return_format' => 'array',
'library' => 'all',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => 'png, svg',
'preview_size' => 'medium',
),
),
),array(
'key' => 'field_floating_bar_item_2',
'label' => 'Item 2',
'name' => 'item_2',
'aria-label' => '',
'type' => 'group',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'layout' => 'table',
'sub_fields' => array(
array(
'key' => 'field_floating_bar_item_2_display',
'label' => 'Display',
'name' => 'item_display',
'aria-label' => '',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui_on_text' => '',
'ui_off_text' => '',
'ui' => 1,
),
array(
'key' => 'field_floating_bar_item_2_link',
'label' => 'Lien',
'name' => 'item_link',
'aria-label' => '',
'type' => 'link',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'return_format' => 'array',
),
array(
'key' => 'field_floating_bar_item_2_image',
'label' => 'image',
'name' => 'item_image',
'aria-label' => '',
'type' => 'image',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'return_format' => 'array',
'library' => 'all',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => 'png, svg',
'preview_size' => 'medium',
),
),
),
),
'location' => array(
array(
array(
'param' => 'page',
'operator' => '==',
'value' => get_page_by_path(sanitize_title($option_page_title))->ID,
),
),
),
'menu_order' => -1,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
'show_in_rest' => 0,
) );
}
add_action( 'acf/init', 'GenerateACFOptions');
The fields are correctly Shown inside the gutenberg editor with my code. Yet, when i save draft or even publish the page and then refresh my page, everything is cleaned. I have other general fields in my website, created inside the ACF admin page, and they are working perfectly fine. When i try to use the get_fields($id_of_the_option_page);
, only the general ACF are shown, not the code generated ones.
Do you have any idea on the problem ?
Thanks for your patience, i will provide more info if needed
You must be logged in to reply to this topic.
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.