How to handle forms from sidebar widgets – Processing $_POST variables using get_field_name()

WP doesn’t provide any helper methods for creating and processing forms in the front-end.

To process your front-end form simply check for $_POST-ed data. I suggest you do this from within an action, and only if the widget is active.

In your constructor – the someWidget() method (which you should rename to __construct, and call parent::__construct instead of WP_Widget), add:

$my_widget = $this;

// this action gets to run once only
add_action('init', function() use($my_widget){

  // give up if there isn't an instance active of this widget
  if(!is_active_widget(false, false, $my_widget->id_base, true))
    return;

  // get widget options from all instances
  $widget_options = get_option($my_widget->option_name);

  // loop trough options of each instance
  foreach((array)$widget_options as $instance => &$options){

    // identify instance
    $id = "{$my_widget->id_base}-{$instance}";

    // this instance is not active, so skip it
    if(!is_active_widget(false, $id, $my_widget->id_base)) continue;

    // here process your form for this specific widget instance
    if(isset($_POST["some_field-{$id}"])){

      // the $options variable holds the instance options
    }

  }

  // if you need to update the options for all instances...
  update_option($my_widget->option_name, $widget_options);

});

In the widget() method, use the $this->id variable to get a unique ID for the current instance, which you can use it in form field names, IDs and so on…