最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Can I use the different settings sections over different pages using the save options group?

programmeradmin8浏览0评论

I registered one options group with few different options sections. Each section is meant for different page.

register_setting(                       // 
        'mhp_options',                  // Options group
        'mhp_plugin_options',           // Database option
        'mhp_plugin_options_validate'   // The sanitization callback,
);

// Manage Menu section
add_settings_section(                   // 
        $mhp_manage_menu_section,   // Unique identifier for the settings section
        __('Manage Menu Settings', 'mhp'),  // Section title
        '__return_false',               // Section callback (we don't want anything)
        $mhp_manage_menu_section            // Menu slug
);
add_settings_section(                   
        $mhp_home_page_layout_settings_section,     
        __('Home Page Layout Settings', 'mhp'),     
        '__return_false',               
        $mhp_home_page_layout_settings_section          
);

Then each page is using

settings_fields( 'mhp_options' ); 
do_settings_sections( $mhp_manage_menu_section);

so the other page is using

settings_fields( 'mhp_options' ); 
do_settings_sections( $mhp_home_page_layout_settings_section);

but it looks like on save only current section is saved in the options table. What ever was already there is deleted.

Do I have to create new options group for each page or is there any other solution?

I registered one options group with few different options sections. Each section is meant for different page.

register_setting(                       // http://codex.wordpress/Function_Reference/register_setting
        'mhp_options',                  // Options group
        'mhp_plugin_options',           // Database option
        'mhp_plugin_options_validate'   // The sanitization callback,
);

// Manage Menu section
add_settings_section(                   // http://codex.wordpress/Function_Reference/add_settings_section
        $mhp_manage_menu_section,   // Unique identifier for the settings section
        __('Manage Menu Settings', 'mhp'),  // Section title
        '__return_false',               // Section callback (we don't want anything)
        $mhp_manage_menu_section            // Menu slug
);
add_settings_section(                   
        $mhp_home_page_layout_settings_section,     
        __('Home Page Layout Settings', 'mhp'),     
        '__return_false',               
        $mhp_home_page_layout_settings_section          
);

Then each page is using

settings_fields( 'mhp_options' ); 
do_settings_sections( $mhp_manage_menu_section);

so the other page is using

settings_fields( 'mhp_options' ); 
do_settings_sections( $mhp_home_page_layout_settings_section);

but it looks like on save only current section is saved in the options table. What ever was already there is deleted.

Do I have to create new options group for each page or is there any other solution?

Share Improve this question edited Oct 7, 2013 at 0:25 Radek asked Oct 7, 2013 at 0:16 RadekRadek 5951 gold badge11 silver badges26 bronze badges 2
  • If you can retrieve all the fields names from outside settings api (e.g. you have saved it in a static class variable or in a global variable) what you ask is possible and easy. Just for clarification for 'field names' I mean the ids you pass as first argument to add_settings_field for fields of all sections. – gmazzap Commented Oct 10, 2013 at 5:23
  • I already found the solution. Before posting it I wanted to give opportunity someone to earn the points. Your way would work, do you want to create an answer out of it? – Radek Commented Oct 11, 2013 at 3:03
Add a comment  | 

2 Answers 2

Reset to default 1

The solution is to merge existing options before saving options in case not all options are presented on the page we are about save.

So in the end of $sanitize_callback in register_setting function just before I return the data I call below function where $existing are all existing options saved in database.

function merge_options( $existing,  $input ) { // Merges existing options with $input to preserve existing options

    if ( !is_array( $existing ) || !is_array( $input ) ) // something went wrong
        return $data;
    return array_merge( $existing, $input );
}

To complement @Radek's answer, here is another way to use his solution.

public function sanitize_callback ( $options )
{
    $saved_options = $this->get_option( 'your_option_name' );
    $options = $this->sanitize_options( $options );

    if ( is_array( $saved_options ) && is_array( $options ) )
    {
        $options = array_merge( $saved_options, $options );
    }

    return $options;
}

The sanitize_callback function is the one passed as an argument in your register_setting, and then you can use another one (named sanitize_options in my case) to do your sanitization.

This sanitize_callback will take care of merging all the options under the same $option_name.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论