I’m a little confused about this, even though I have read all of the API and searched for hours.
When I activate my plugin I add some values to the options database, e.g.
add_option('code','24');
How do I update that value or use it in the widget? I only see “instances” now, like the example on this page:
http://wpcoderz.com/creating-a-multi-instance-widget-with-wordpress-2-8/
I don’t really understand what instances are, the WP documentation doesn’t provide much explanation about it.
Instance
‘instance’ is really ment as in “object oriented programming instance’: You have a certain class (which is a certain widget you coded as a class) and the moment you use it on a site, you create a specific instance of it; you create an object you can manipulate. BUT you can have one instance that has e.g. value “hello” as header and another one that has “hello world” as header (same class different instances)(so 2 widgets of the same class but with different properties).
Values in these widgets
If you have values that are specific to an instance you would specificy them in the widget class code, so you can e.g. show them in a form to the end user to manipulate. Dont worry about saving to the database, the widget class in the background takes care of that.
If you have values that are the same for all instances of that widget you maybe could grab that from an option you set but they will never show up to the user in a form.
(as you know with add_option you add yourself a new option to the table wp_options.)
with widgets there is an intermediate layer (the widget factory) that saves the widget options for you in this wp_options table, so you do not have to manually add these options and updates on these options, to see this open wp_options and walk through the content of this table.
if you nevertheless want to have access to your own option value you should add the regular option code inside the widget code (since then this setting can be changed inside the widget screen instead of e.g. your own plugin screen).
If it is a value you would like to use directly maybe it is easier to not use your own option value but just use the one in the widget.
If you use it to manipulate another value maybe you can better add a filter instead and then hook into this filter to manipulate the value used in the widget (as opposed to reading the option value directly)
So first you construct a widget e.g.:
Then, in the widget method you can set initial values e.g. look at $title and you see that it first looks if THIS instance of the widget ( meaning the specific widget of this type that was dragged to the widget bar) already set a title, if not then it assigns a default value. IF you want to use your default value from some option you saved in the database you can replace the fixed value with one that you read from the option table, but why should you? You also see the value “25” below which is pretty hard coded, maybe that would be a good value to fetch from a database options table that you set yourself in a plugin admin screen (so replace 25 with get option)
The update method just updates the values if they have changed.
The form method displays the form in the admin screen of the widget.