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?
Fetch current options, and be sure to return an array if empty:
Ensure desired options are in an array:
Merge them, using
[wp_parse_args()
]1:Update
Of course, the trick for you is ensuring that you’re dealing with an array with your desired options. Maybe do something like so:
(P.S. please do some sort of sanitization.
$_POST
data are inherently untrustworthy.)Edit
Just follow the process laid out above:
Fetch current options, and be sure to return an array if empty:
Ensure desired options are in an array:
Merge them, using
[wp_parse_args()
]1:Update