admin管理员组文章数量:1312844
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.
本文标签: Updating just one option with updateoption
版权声明:本文标题:Updating just one option with update_option 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741904574a2404067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论