Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted
  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 );
    
share|improve this answer
    
Hm, @ChipBennett, Unfortunately it doesn't seem to work, it doesn't update any options. I've tried using multidimensional array as $current_options and it's the same. I will post an update soon. –  Wordpressor Oct 30 '13 at 0:14
    
Without knowing the structure of your data stored as an option, or the structure of the $_POST data, it's impossible to provide a more-specific solution. –  Chip Bennett Oct 30 '13 at 1:08
    
I'm not using $_POST data for now, just this simple array as in example (if it will work then anything will, I hope). I have around 30 options, inputs, booleans, textareas. They're all stored under XX_theme_settings and one of them is called existing_field (literally :)). I guess it isn't enough data? Maybe I could merge them using PHP functions somehow, not wp_parse_args()? I want this to be universal to be honest, working with many themes / any data. I'm going to bed as it's 3 am here, I will reply tomorrow. Goodnight :) –  Wordpressor Oct 30 '13 at 1:50
    
The wp_parse_args() method will work just fine. See updated answer. –  Chip Bennett Oct 30 '13 at 1:57
    
Does the code from your edit differ from the one from my edit? I'm afraid id still does nothing, I will create new theme from the scratch and try this, maybe there's something wrong with mine. I'm 100% positive I'm using right field name and option name. –  Wordpressor Oct 30 '13 at 14:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.