update_option is not working!

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] Arrayn(n    [brandcaster_id] => 12222222222222n    [maxpost] => n    [status] => 1n    
		
=> 2n [parent_category] => 5n [tags] => BETTER WORK3, %majcat%, %mincat%, %brand%n [enable_featured] => enable_featuredn [html] => HTML goes heren [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] Arrayn(n [brandcaster_id] => 12222222222222n [maxpost] => n [status] => 1n
=> 2n [parent_category] => 5n [tags] => BETTER WORK3, %majcat%, %mincat%, %brand%n [enable_featured] => enable_featuredn [html] => HTML goes heren [style] => n [testing] => Testing1234n)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] Arrayn(n [brandcaster_id] => 12222222222222n [maxpost] => n [status] => 1n
=> 2n [parent_category] => 5n [tags] => BETTER WORK3, %majcat%, %mincat%, %brand%n [enable_featured] => enable_featuredn [html] => HTML goes heren [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.

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

update_option is not working!

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.