Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress get_option returning false

I have wordpress plugin which register settings like:

register_settings("myplugin_settings","myplugin_option1");

and on plugin activation it's adding options like:

add_option("myplugin_option1","");

Then if I update option, like:

update_option("myplugin_option1","something else");

and later try to get it from database:

$myoption = get_option("myplugin_option1");

get_option() returns false, even option exist and value is updated.

I know that value is updated and option exist because when I run query to get option I am getting the value:

global $wpdb;
$query = 'SELECT * FROM wp_options WHERE option_name = "myplugin_option1";
$result = $wpdb->get_results($query);

This returns me value: "something else"

For example this code:

 $option = get_option("myplugin_option1");
 if(false == $option){
    global $wpdb;
    $query = 'SELECT * FROM wp_options WHERE option_name = "myplugin_option1"';
    $result = $wpdb->get_results($query);
 }

After this code is executed, $result will contain value:

Array
(
   [0] => stdClass Object
      (
        [option_id] => 11752
        [option_name] => myplugin_option1
        [option_value] => something else
        [autoload] => yes
      )
)

So option exist and has value but get_option() is returning false.

I have this problem only with 3 options and only on one site where my plugin is installed. Does anyone has some idea why get_option() returns false even option is in database and has value "something else".

like image 337
carpics Avatar asked Apr 28 '15 03:04

carpics


1 Answers

The issue with this was that those three options with which I had problems was somehow cached twice in wp cache.

So one options was cached twice, and update_options() was always updating one of these two and get_option() was always getting the another one cached with empty value.

Not sure how this happened, but clearing cache resolved my issue.

like image 155
carpics Avatar answered Nov 03 '22 01:11

carpics