0

I have a call to update_option() inside a class which edits some keys from the options array passed to it and then saves the updated array to the database, I don't have any fancy code, the function is only calling the update_option() function like this:

update_option( 'my_options', $this->updated_options );

Now this class is initialized in an admin page:

$instance = new My_Class($options_to_update);

$instance->init(); // the function that updates the option is called here

The thing is that update_option() is returning true and updating the option with an empty value.

also I checked the array just before the call to this function and nothing is wrong:

print_r( $this->updated_options );

I also tried to update with a string:

update_option( 'my_options', 'any string' );

Now my questions are:

  • Am I doing something wrong ?

  • Is there a specific time to call update_option() ? because I have it in a function hooked to admin_init and it's working fine.

Update:

I found out that it's not working only if i's called inside the display function of add_menu_page() any reason why ? the passed value is always null.

  • And you are not using 'pre_update_option_' . $option filter, somwhere in your code ?, to make sure you are getting proper value for function call, you can check the value received on filter 'pre_update_option_' . $option – sven Dec 30 '13 at 14:07
  • @sven I didn't know about that, I just tried it and dumped the first parameter $new_value and it's showing null, I also tried it with a hard coded string. – Pierre Dec 30 '13 at 14:22
  • did you tried the same outside the class?, Also if it works properly outside the class and not in class, you would need to provide the code. – sven Dec 30 '13 at 14:25
  • @sven yes, I have another class which adds a hook to admin_init to add the default value if this option doesn't exist or empty, and it's working fine. – Pierre Dec 30 '13 at 14:28
  • 1
    Post the code for your class. It is impossible to see what is happening. – s_ha_dum Dec 30 '13 at 15:09

Your Answer

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

Browse other questions tagged or ask your own question.