How to save values to an array?

WordPress Plugin Development: How can I use Settings API to save submitted values to the option table with a specified array key without destroying the existing other keys in the array?

  • This is a very simple plugin which uses Settings API. <?php /* Plugin Name: Settings API Page 1 */ add_action('admin_init', 'settingsapi_init01' ); function settingsapi_init01(){ register_setting( 'my_option_group', 'my_option_name', 'validation_func01' ); add_settings_section( 'section_demo', 'Section Demo', 'section_description_func01', 'setting_api_page01' ); add_settings_field( 'field01', 'Field 01', 'field01_field_func', 'setting_api_page01', 'section_demo' ); } function field01_field_func() { $options = (array) get_option('my_option_name'); echo '<textarea name="my_option_name[setting_api_page01][field01]" cols="100" rows="8">'; echo isset( $options['setting_api_page01']['field01'] ) ? trim($options['setting_api_page01']['field01']) : '' ; echo '</textarea>'; } add_action('admin_menu', 'settings_api_demo_admin_page01'); function settings_api_demo_admin_page01() { add_options_page( 'Settings API Page 1', 'Settings API Page 1', 'manage_options', 'setting_api_page01', 'field01_adminpage_func01'); } function field01_adminpage_func01() { ?> <div class="wrap"> <?php screen_icon(); ?> <h2>Settings API Page 1</h2> <form action="options.php" method="post"> <?php settings_fields('my_option_group'); do_settings_sections('setting_api_page01'); submit_button(); ?> </form> </div> <?php // display the saved options $key = 'my_option_name'; echo 'Option Key: ' . $key . '<pre>' . print_r( get_option( $key ), true) . '</pre>'; } function section_description_func01() { echo '<p>' . __FUNCTION__ . '</p>'; } function validation_func01( $a ) { return $a; } It uses the option key named, my_option_name, and the submitted values are stored as an array. Now I have another plugin that I want it to use the same option key, my_option_name, with Settings API. However, the API deletes the other key used by the other plugin. <?php /* Plugin Name: Settings API Page 2 */ add_action('admin_init', 'settingsapi_init02' ); function settingsapi_init02(){ register_setting( 'my_option_group', 'my_option_name', 'validation_func02' ); add_settings_section( 'section_demo', 'Section Demo', 'section_description_func02', 'setting_api_page02' ); add_settings_field( 'field02', 'Field 02', 'field02_field_func', 'setting_api_page02', 'section_demo' ); } function field02_field_func() { $options = (array) get_option('my_option_name'); echo '<textarea name="my_option_name[setting_api_page02][field02]" cols="101" rows="9">'; echo isset( $options['setting_api_page02']['field02'] ) ? trim($options['setting_api_page02']['field02']) : '' ; echo '</textarea>'; } add_action('admin_menu', 'settings_api_demo_admin_page02'); function settings_api_demo_admin_page02() { add_options_page( 'Settings API Page 2', 'Settings API Page 2', 'manage_options', 'setting_api_page02', 'field02_adminpage_func02'); } function field02_adminpage_func02() { ?> <div class="wrap"> <?php screen_icon(); ?> <h3>Settings API Page 2</h3> <form action="options.php" method="post"> <?php settings_fields('my_option_group'); do_settings_sections('setting_api_page02'); submit_button(); ?> </form> </div> <?php // display the saved options $key = 'my_option_name'; echo 'Option Key: ' . $key . '<pre>' . print_r( get_option( $key ), true) . '</pre>'; } function section_description_func02() { echo '<p>' . __FUNCTION__ . '</p>'; } function validation_func02( $a ) { return $a; } Activate these two plugins at the same time and you'll see the problem.

  • Answer:

    Figured it out. Merging the passed option array with the original option array in the validation function works. function validation_func01( $a ) { $arr = (array) get_option( 'my_option_name' ); $a = $a + $arr; return $a; } function validation_func02( $a ) { $arr = (array) get_option( 'my_option_name' ); $a = $a + $arr; return $a; }

Anonymous at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.