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.


How do I update that value or use it in the widget? I only see "instances" now, like the example on this page:


I don't really understand what instances are, the WP documentation doesn't provide much explanation about it.

1 Answer 1



'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.:

     * Constructor
    function __construct()
    $widget_ops = array('classname' => 'widget_icons_latest', 'description'
        => __('Shows your latest ♥ WP-Favicons which corresponds (after some time caching) '.
        'with the latest outgoing links you published on your website. You can style '.
        'the icons by using the stylesheet in the text widget settings under the ♥ WP-favicons '.
        'Context settings.'));
    $control_ops = array('width' => 200, 'height' => 350);
    parent::__construct('WPFaviconsLatest', __('♥ Show Latest WP-Favicons'), $widget_ops, $control_ops);

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)

     * (non-PHPdoc)
     * @see WP_Widget::widget()
    function widget($args, $instance) {
        extract( $args );
        echo $before_widget;
        $title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
        $amount = empty($instance['amount']) ? 25 : $instance['amount'];
        $instance['text'] = $this->specificOutput($amount, false);
        $text = apply_filters( 'widget_text', $instance['text'], $instance );
        if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
        <div class="widget_icons_latest"><?php echo $text; ?></div>
        <div class="widget_icons_latest">by <a href="http://wordpress.org/extend/plugins/wp-favicons/">WP-Favicons</a></div>
        echo $after_widget;

The update method just updates the values if they have changed. The form method displays the form in the admin screen of the widget.

  • How is this data saved? And can it be easily retrieved?
    – crosenblum
    Dec 2, 2011 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.