1

I am writing a plugin and I need to use the update_option function to update an array I have stored as an option in WP. For the admin panel I used the settings API but for this portion I need to use the update_option directly.

I have simplified my code so all it is currently doing is reading in the option, adding an element to the returned array, and updating the option with the modified array. But when I read the option back in it does no contain the new element.

Do I have to do something to inform the settings API that I will be using this new element (that I don't want to be visible in the admin panel)? Have I missed something else?

I do not know where to troubleshoot beyond this point. The update_option function does not appear to be working and I have no idea why! Any suggestions/pointers/troubleshooting ideas you can pass my way will be greatly appreciated!

Below is code from my plugin:

$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 );

Below is the output from my error logs from this code:

[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

You can see from the error_log that the $coupon_grab_options array does have the element testing present before I pass it to update_option but if I read in the option after the update it is not present.

  • What's the context of this code, when does it run? – Milo Jan 14 '17 at 19:12
  • It is being called asynchronously each time wp_head is called. I have a delay that prevents it being called more than 30 seconds apart (30 seconds is just for testing). I'm using the TechCrunch WP Asynchronous Tasks class (github.com/techcrunch/wp-async-task) to launch the async call. – Nick Powers Jan 14 '17 at 19:20
  • Have you verified that the problem exists without using the async task plugin? – Milo Jan 14 '17 at 19:26
  • I was able to get this working by using another single option, one that was not used in conjunction with the settings API and is not an array. I'm not sure what the magic sauce was there but it works so I'm happy! :) Thanks for your replies @Milo – Nick Powers Jan 14 '17 at 19:41
0

I was able to get this working by using a separate option, that is not an array. I hate having to use multiple options like this but it works so I am going to go with it. My guess is that using update_option is not supported after implement the option with the settings API.

  • Also continuing where we left on under your other question, I now recollect a somewhat similar situation for me, in which a value under an otherwise fully functional option array would not update, forcing me to add a simple additional option. I've also recently encountered a peculiar, contrary situation where an array returned by a function would not work until retrieved from an option. Getting to the bottom of such apparent exceptions woudl seem to require study of the options table and functions. – CK MacLeod Jan 15 '17 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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