0

Thanks to Wordpress calling their content 'posts', it makes it hard to find information on dealing with forms submitted with the POST protocol. My question is simple, the answer should be simple to a seasoned Wordpress developer.

Using a Wordpress plugin which has a form in the widget method of the class, I named the variables using the Wordpress get_field_name function. I can't find the corresponding function that determines what $_POST variable to process. I could kludge a solution, but I want to use the Wordpress framework exclusively for this.

I need to maintain the class variable structure so that multiple instances of this widget don't have variable overlap. I could simply set the name and used the widget_id from the args, but that doesn't seem to be the correct way to stay within the framework.

To clarify further, the widget delivers a form for the user side, not the admin side.

Example:

class someWidget extends WP_Widget {
 public function someWidget() {
  $this->WP_Widget('somewidget', __('Some Widget'));
 }

 public function form($instance) {
 }

 public function update( $new_instance, $old_instance ) {
  $instance = $old_instance;
  return $instance;
 }

 public function widget($args, $instance) {
  $before_title = $before_widget = $after_widget = $after_title = '';
  extract($args, EXTR_IF_EXISTS);
  echo $before_widget.$before_title.$instance['WidgetTitle'].$after_title;

  $somewidget_variable = $_POST['????']; // < -- What function retreives the variable?
  if ($somewidget_variable) {
   // amazing stuff
  }
?>
<form method="POST">
<input id="<?php echo $this->get_field_id('somewidget_variable');?>" name="<?php echo $this->get_field_name('somewidget_variable');?>" type="text" value="<?php echo $somewidget_variable;?>" />
<input type="submit" />
</form>
<?php
echo $after_widget;
 }
}

2 Answers 2

1

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

3
  • Unless I missed something, those methods are for the admin side only. I'm only asking about the user/public side, that is why the form is in the widget method.
    – Exit
    Apr 9, 2012 at 18:52
  • Well, that line you just added in your Q changed it completely Apr 9, 2012 at 19:49
  • The title says 'Processing $_POST variables using get_field_name()' and the example code asks specifically 'What function retreives the variable?', but I can see how someone might assume this is set up incorrectly as you did. So, any clues as to the answer?
    – Exit
    Apr 10, 2012 at 17:21
1

Okay, I've looked at the function description for get_field_name() and it says that this function is specifically designed to be used in the form method of the Widget class. I found no uses in the Wordpress code outside of the form() method.

/**
 * Constructs name attributes for use in form() fields
 *
 * This function should be used in form() methods to create name attributes for fields to be saved by update()
 *
 * @param string $field_name Field name
 * @return string Name attribute for $field_name
 */
function get_field_name($field_name) {
    return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';
}

Looking at the actual code, it is concatenating variables from the widget, so technically, you could use this function to create a field, but it wouldn't be proper to the framework. It's best just to create your own field names and serialize using the widget ID.

So the solution is as follows:

class someWidget extends WP_Widget {
 public function someWidget() {
  $this->WP_Widget('somewidget', __('Some Widget'));
 }

 public function form($instance) {
 }

 public function update( $new_instance, $old_instance ) {
  $instance = $old_instance;
  return $instance;
 }

 public function widget($args, $instance) {
  $before_title = $before_widget = $after_widget = $after_title = '';
  extract($args, EXTR_IF_EXISTS);
  echo $before_widget.$before_title.$instance['WidgetTitle'].$after_title;

  $somewidget_variable = $_POST['somewidget_'.$this->id];
  if ($somewidget_variable) {
   // amazing stuff
  }
?>
<form method="POST">
<input id="somewidget_<?php echo $this->id;?>" name="somewidget_<?php echo $this->id;?>" type="text" value="<?php echo $somewidget_variable;?>" />
<input type="submit" />
</form>
<?php
echo $after_widget;
 }
}

Your Answer

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

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