update_option is not working!

时间:2017-01-14 作者:Nick Powers

我正在编写一个插件,我需要使用update\\u option函数来更新我作为选项存储在WP中的数组。对于管理面板,我使用了设置API,但对于这部分,我需要直接使用update\\u选项。

我已经简化了代码,所以它当前所做的就是读入选项,向返回的数组中添加元素,并使用修改后的数组更新选项。但当我在后面阅读该选项时,它不包含新元素。

我是否必须做些什么来通知设置API我将使用这个新元素(我不想在管理面板中可见)?我还错过了什么吗?

除此之外,我不知道在哪里进行故障排除。update\\u option功能似乎不起作用,我不知道为什么!如果您能给我提供任何建议/建议/疑难解答,我将不胜感激!

下面是我插件中的代码:

$coupon_grab_options = get_option( \'coupon_grab_option\' );

error_log( "Dump of coupon_grab_options before update", 0 );
error_log( print_r( $coupon_grab_options, true ), 0 );
error_log( "_______________________________", 0 );

$coupon_grab_options[\'testing\'] = "Testing1234";

error_log( "Dump of array coupon_grab_options before passing it as an argument to update_option");
error_log( print_r( $coupon_grab_options, true ), 0 );
error_log( "_______________________________", 0 );

wp_cache_delete ( \'alloptions\', \'options\' );
$test = update_option( \'coupon_grab_option\', $coupon_grab_options );

$booltest = ($test) ? \'true\' : \'false\';
error_log( "update_option returned a $booltest value", 0 );

wp_cache_delete ( \'alloptions\', \'options\' );
$coupon_grab_options = get_option( \'coupon_grab_option\' );

error_log( "Dump of coupon_grab_options after update", 0 );
error_log( print_r( $coupon_grab_options, true ), 0 );
下面是此代码的错误日志输出:

[Sat Jan 14 18:58:47.213079 2017] [:error] [pid 9414] [client 67.205.159.176:33378] Dump of coupon_grab_options before update
[Sat Jan 14 18:58:47.215500 2017] [:error] [pid 9414] [client 67.205.159.176:33378] Array\\n(\\n    [brandcaster_id] => 12222222222222\\n    [maxpost] => \\n    [status] => 1\\n    [author] => 2\\n    [parent_category] => 5\\n    [tags] => BETTER WORK3, %majcat%, %mincat%, %brand%\\n    [enable_featured] => enable_featured\\n    [html] => HTML goes here\\n    [style] => \\n)\\n
[Sat Jan 14 18:58:47.215675 2017] [:error] [pid 9414] [client 67.205.159.176:33378] _______________________________
[Sat Jan 14 18:58:47.215952 2017] [:error] [pid 9414] [client 67.205.159.176:33378] Dump of array coupon_grab_options before passing it as an argument to update_option
[Sat Jan 14 18:58:47.216101 2017] [:error] [pid 9414] [client 67.205.159.176:33378] Array\\n(\\n    [brandcaster_id] => 12222222222222\\n    [maxpost] => \\n    [status] => 1\\n    [author] => 2\\n    [parent_category] => 5\\n    [tags] => BETTER WORK3, %majcat%, %mincat%, %brand%\\n    [enable_featured] => enable_featured\\n    [html] => HTML goes here\\n    [style] => \\n    [testing] => Testing1234\\n)\\n
[Sat Jan 14 18:58:47.216256 2017] [:error] [pid 9414] [client 67.205.159.176:33378] _______________________________
[Sat Jan 14 18:58:47.221790 2017] [:error] [pid 9414] [client 67.205.159.176:33378] Dump of coupon_grab_options after update
[Sat Jan 14 18:58:47.221961 2017] [:error] [pid 9414] [client 67.205.159.176:33378] Array\\n(\\n    [brandcaster_id] => 12222222222222\\n    [maxpost] => \\n    [status] => 1\\n    [author] => 2\\n    [parent_category] => 5\\n    [tags] => BETTER WORK3, %majcat%, %mincat%, %brand%\\n    [enable_featured] => enable_featured\\n    [html] => HTML goes here\\n    [style] => \\n)\\n
您可以从错误日志中看到,$优惠券\\u grab\\u选项数组在我将其传递给update\\u选项之前确实存在元素测试,但如果我在更新后读入该选项,则它不存在。

1 个回复
SO网友:Nick Powers

我可以通过使用一个单独的选项来实现这一点,该选项不是数组。我讨厌像这样使用多个选项,但它是有效的,所以我将继续使用它。我的猜测是,在使用settings API实现选项后,不支持使用update\\u选项。