0

Each widget has a row in the options table with values for title and other fields. Is there any way we can update those values outside the update function?

For example, here are some values for a sample widget:

a:2:{i:2;a:23:{s:5:"title";s:19:"Widget Title";.....

now I want to update the title to another value in the function widget($args, $instance) and save the value to the database.

I found that options API can handle something like this, but I am not sure if this is the correct way and how to update the info for that specific widget.

// Update the value of an option that was already added.
update_option( $option, $newvalue );
  • why would you need to update the value without using update_option() ? – bynicolas Aug 24 '16 at 17:25
  • @bynicolas if that is the way to get what I want then I would use it, but I have never used that function and I dont know how to update the options for that specific widget. – Rain Man Aug 24 '16 at 17:28
0

An array will be serialized when saved to the DB. If you want to update a key to the array, you need to

  • Fetch the option first
  • Apply modifications
  • Update the option with the updated array values

so in your example if you fetch the option, you should get a multidimensional array where you have a key => value of [title] => Widget Title you would update that title key with your new value and then update back your option.

Something like this

$my_option = get_option( 'your_option_name' );

now let's say your key is in the first dimension of the array, then you update your value there.

$my_option['title'] = 'New Widget Title';

and finally you update your options back to the DB.

update_option( 'your_option_name', $my_option );
| improve this answer | |
  • Maybe posting some code will help, what I posted is the way to get serialized data from DB and saving it back ( generally used for plugins settings ). I believe you need to use the update method for that which takes into consideration your different instances – bynicolas Aug 24 '16 at 17:58
  • so it doesnt replace the existing one? because using this method, it just adds another title with the new value in the database and does not update the old one. Here is the unserialized data – Rain Man Aug 24 '16 at 18:08
  • I am not sure, seeing some code would make it clearer what you are trying to achieve. to update the old title using update_option you need to overwrite the correct key in the option array. using print_r() to dump your option variable would make it clearer for you if you are edition the correct key – bynicolas Aug 24 '16 at 18:13
  • sorry about that, I missed the link. it looks like you are updating $my_option['title'] when you should be updating $my_option[2]['title']. could that be it? – bynicolas Aug 24 '16 at 18:21
  • Let us continue this discussion in chat. – bynicolas Aug 24 '16 at 18:32
0

The widget() method doesn't seem like a good choice for such operation, since it handles display of the widget. The update() method is where you typically manipulate the widget instance, in that case you don't have to explicitly save it since base class takes care of that.

In more general view what you are seeing is serialized data. Anything you save to option that isn't string already gets converted into such serialized string for storage. You can change it, as long as you are sure that's what you are actually want to do.

As long as you stick to Options API, it will transparently unserialize data on retrieval and serialize on save.

If you do this directly you will have to handle this yourself.

Simply changing serialized string breaks it, since the format depends on capturing length of data and mismatches stop it from working. That is very typical mistake in migrations, when people search/replace strings without accounting for this.

| improve this answer | |
  • thanks for your comment, I actually asked a question here and this is my way of solving the problem to that question. – Rain Man Aug 24 '16 at 17:43

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.