There are many options within an 'option' in the 'wp_options' table. This option, second_option
, contains three parameters and consequently has three values. The values are, for example, as follows:
{s:12:"manual";s:0:""; s:8:"currency";s:3:"USD";s:14:"state";s:2:"nn";}
I want to update the value of the second parameter, and that is 'currency'
, yet whenever I want to update just this portion, Wordpress just updates whole of the option and the first option and the third, that is 'manual'
and 'state'
are deleted and just remains what I am updating, that is 'currency'
. I use the following piece of code:
$sets = get_option( 'second_option' );
$sets['currency'] = 'some_value';
update_option('second_option', $sets);
Is there a way to update just the option 'currency'
without affecting other parameters, that is 'manual'
and 'state'
?
Thanks in advance.
There are many options within an 'option' in the 'wp_options' table. This option, second_option
, contains three parameters and consequently has three values. The values are, for example, as follows:
{s:12:"manual";s:0:""; s:8:"currency";s:3:"USD";s:14:"state";s:2:"nn";}
I want to update the value of the second parameter, and that is 'currency'
, yet whenever I want to update just this portion, Wordpress just updates whole of the option and the first option and the third, that is 'manual'
and 'state'
are deleted and just remains what I am updating, that is 'currency'
. I use the following piece of code:
$sets = get_option( 'second_option' );
$sets['currency'] = 'some_value';
update_option('second_option', $sets);
Is there a way to update just the option 'currency'
without affecting other parameters, that is 'manual'
and 'state'
?
Thanks in advance.
Share Improve this question asked Dec 19, 2020 at 13:50 H. M..H. M.. 1135 bronze badges1 Answer
Reset to default 1Your method is correct, but the problem is that the value inside the option in your example, i.e.:
get_option( 'second_option' );
is not serialized correctly ( it was either edited directly--and incorrectly--through the database or inserted with something other than update_option()
).
If you var_dump( get_option( 'second_option' ) );
, you'll see that it's not an array but a string.
The correct serialized value in the database for your example would be:
a:3:{s:6:"manual";s:0:"";s:8:"currency";s:3:"USD";s:5:"state";s:2:"nn";}
Using that, your approach will work as expected. However, instead of editing this manually (as it is prone to errors, like in this example), just do a reset of the option like so:
$second_option = array(
'manual' => '',
'currency => 'USD',
'state' => 'nn',
);
update_option( 'second_option', $second_option );
Afterwards, this will work as expected:
$sets = get_option( 'second_option' );
$sets['currency'] = 'some_value';
update_option( 'second_option', $sets );
PS: There is no need to unserialize or serialize when using get_option() and update_option() respectively, these functions take care of that.