Settings API – how to update multiple options manually?

I’m storing all my options using Settings API:

function registerSettings() {

register_setting('XX_theme_settings', 'XX_theme_settings', 'setting_validate' );

add_settings_section('theme_options', 'Theme Options', 'theme_options_generate', 'page1' ); 
add_settings_field( 'XX_Option1', 'Option 1', 'text_input', 'page1', 'theme_options', 'XX_Option1' );
add_settings_field( 'XX_Option2', 'Option 2', 'text_input', 'page1', 'theme_options', 'XX_Option2' ); 

};

add_action('admin_init', 'registerSettings');

Let’s say I want to update all the options manually (not using forms, but via PHP), how should I do that?

Read More

That’s how this update looks like for one setting:

$my_options= get_option('XX_theme_settings');//retrieve all options
$my_options['XX_Option2'] = 'my new value'; //amend value in array(s)
update_option('XX_theme_settings', $my_options); //update option array

I have absolutely no idea how to make it work for multiple (originally I have over 50) setting fields.

I’ve been trying this way:

$current_options = get_option('XX_theme_settings');
$imported_options = array_merge($current_options , array_intersect_key($_POST["import"], $current_options ));
// $imported_options = $_POST["import"]; - simplified
update_option('XX_theme_settings', $imported_options);

And it does not work, of course “import” is not a string but an array.

Here’s a related (my own) question: Settings API – how to update options manually?

I guess I still do need to create a PHP foreach loop and loop through all the settings, then list them, ten add every single one via update_option? How to? Would it be slow, because it sounds like it?

UPDATE

This code doesn’t seem to update the settings:

$current_options = get_option( 'XX_theme_settings', array() );
$desired_options = array("existing_field" => "new value");
// $desired_options = array("XX_theme_settings" => array("existing_field" => "new value")); tried this one, but also no luck
$merged_options = wp_parse_args( $current_options, $desired_options );
update_option( 'XX_theme_settings', $merged_options );

UPDATE II

This is even stranger.

I put the above code at page.php (just for testing reasons):

    $current_options = get_option( 'XX_theme_settings', array() );
    $desired_options = array("existing_field" => "new value");
    $merged_options = wp_parse_args( $current_options, $desired_options );
    update_option( 'XX_theme_settings', $merged_options );

I refresh one of the pages. Nothing happens. Then I change the code to this:

    $current_options = get_option( 'XX_theme_settings', array() );
    $desired_options = array("existing_field" => "new value");
    $merged_options[] = wp_parse_args( $current_options, $desired_options ); //here's the change
    update_option( 'XX_theme_settings', $merged_options );

Another refresh, “existing_field” is now emtpy.

Then I once again paste the first code and refresh for the third time and it works.

Hint: maybe I should remove fields value before updating them?

Related posts

1 comment

    1. Fetch current options, and be sure to return an array if empty:

      $current_options = get_option( 'option_name', array() );
      
    2. Ensure desired options are in an array:

      $desired_options = array( /* whatever you need goes here */ );
      
    3. Merge them, using [wp_parse_args()]1:

      $merged_options = wp_parse_args( $current_options, $desired_options );
      
    4. Update

      update_option( 'option_name', $merged_options );
      

    Of course, the trick for you is ensuring that you’re dealing with an array with your desired options. Maybe do something like so:

    $desired_options = array( $_POST['import'] );
    

    (P.S. please do some sort of sanitization. $_POST data are inherently untrustworthy.)

    Edit

    I have around 30 options, inputs, booleans, textareas. They’re all
    stored under XX_theme_settings and one of them is called
    existing_field

    Just follow the process laid out above:

    1. Fetch current options, and be sure to return an array if empty:

      $current_options = get_option( 'XX_theme_settings', array() );
      
    2. Ensure desired options are in an array:

      $desired_options = array( 'existing_field' => 'your string here' );
      
    3. Merge them, using [wp_parse_args()]1:

      $merged_options = wp_parse_args( $current_options, $desired_options );
      
    4. Update

      update_option( 'XX_theme_settings', $merged_options );
      

Comments are closed.