Saving widget options from class method

Why are you writing your own save handlers for widgets? The update method your widget class will take care of this. You’re saving per widget settings, exactly what the Widget API was designed to do.

<?php
class WPSE76224_Widget extends WP_Widget
{
    public function __construct()
    {
        // Create the widget options and such here, then call
        // $this->WP_Widget to let wordpress know about it.
    }

    public function widget($args, $instance)
    {
        // display the widget on the front end.
    }

    public function form($instance)
    {
        // The widget form in the admin area.
    }

    public function update($new, $old)
    {
        // Saves your data, called via AJAX. But your saving logic here.
    }
}

Since WordPress “loads” the widget class (you just register it with register_widget). Chances are an instance of of the widget class is not created when you think it is — on ajax requests, for instance. If you do want to write your ajax handlers, you’ll likely have to move that functionality outside of the widget class.

Further reading:

  1. http://codex.wordpress.org/Widgets_API
  2. http://pmg.co/how-to-build-your-own-wordpress-widgets This is an older tutorial I wrote. The “fetching the tweet” part doesn’t work anymore due to changes a Twitter.