Last updated on September 19, 2023

ACF options page not saving data

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Troubleshooting ACF Options Page Data Saving.

The code snippet provided below demonstrates how to create an options page using Advanced Custom Fields (ACF) in WordPress and ensure that the data is saved correctly.

// Register the options page
if( function_exists('acf_add_options_page') ) {
    acf_add_options_page(array(
        'page_title'    => 'Theme Options',
        'menu_title'    => 'Theme Options',
        'menu_slug'     => 'theme-options',
        'capability'    => 'edit_posts',
        'redirect'      => false
    ));
}

// Save options page data
function wpsnippets_save_options_page() {
    if( function_exists('acf_save_post') ) {
        acf_save_post( $_POST['acf'], 'options' );
    }
}
add_action('acf/save_post', 'wpsnippets_save_options_page', 20);

This code snippet is useful when you want to create an options page using ACF and ensure that the data entered by users is saved correctly. The acf_add_options_page() function is used to register the options page, where you can define the page title, menu title, menu slug, capability, and redirect settings.

To save the options page data, we use the acf_save_post() function within the wpsnippets_save_options_page() function. This function takes two parameters: $_POST['acf'] contains the submitted data, and 'options' specifies that the data should be saved as options.

Finally, we hook the wpsnippets_save_options_page() function to the acf/save_post action using add_action(). This ensures that the function is executed when the options page is saved.

By implementing this code snippet, you can create an ACF options page and ensure that the data entered by users is saved correctly.

Examples

Example 1: Saving data to ACF options page using acf_add_options_page

This example demonstrates how to save data to an ACF options page using the acf_add_options_page function. The code registers an options page and saves a custom field value to it.

function wpsnippets_save_data_to_options_page() {
    $value = 'Hello, World!';
    update_field('field_name', $value, 'option');
}
add_action('acf/init', 'wpsnippets_save_data_to_options_page');

Explanation: The acf_add_options_page function is used to register an options page in ACF. The update_field function is then used to save a custom field value to the options page. The 'option' parameter specifies that the value should be saved as an option rather than a post meta.

Example 2: Saving data to ACF options page using acf/save_post

This example demonstrates how to save data to an ACF options page using the acf/save_post hook. The code checks if the current post is the options page and saves a custom field value to it.

function wpsnippets_save_data_to_options_page($post_id) {
    if ($post_id === 'options') {
        $value = 'Hello, World!';
        update_field('field_name', $value, 'option');
    }
}
add_action('acf/save_post', 'wpsnippets_save_data_to_options_page');

Explanation: The acf/save_post hook is triggered when a post is saved in ACF. The code checks if the current post is the options page by comparing the $post_id parameter to 'options'. If it is, the update_field function is used to save a custom field value to the options page.

Example 3: Saving data to ACF options page using acf_form

This example demonstrates how to save data to an ACF options page using the acf_form function. The code displays a form on the options page and saves the submitted data to it.

function wpsnippets_save_data_to_options_page() {
    acf_form(array(
        'post_id' => 'options',
        'field_groups' => array('group_123'),
        'submit_value' => 'Save',
    ));
}
add_action('acf/init', 'wpsnippets_save_data_to_options_page');

Explanation: The acf_form function is used to display a form on the options page. The 'post_id' parameter is set to 'options' to specify that the form should save data to the options page. The 'field_groups' parameter specifies the ACF field group(s) to display in the form. The 'submit_value' parameter sets the label of the submit button.

Last updated on September 19, 2023. Originally posted on September 19, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *