Resetting plugin settings

As promised in a previous post, I wanted to write about resetting WordPress plugin settings. 
The mechanism for updating the settings that I outlined in that post does allow for [...]

As promised in a previous post, I wanted to write about resetting WordPress plugin settings.

The mechanism for updating the settings that I outlined in that post does allow for resetting them since the options.php code does not support that. Therefore, there are a few things that we need to do differently to allow for easy resetting of the plugin options. There are two main things that we have to provide – 1. a way for the user to signal the resetting of the options and 2. the method by which that request will be received and processed.

The first part is pretty easy, we just add another button to the bottom of our Settings page, which when clicked will tell our plugin that the user has requested the resetting of all the settings. This is accomplished by adding the following code to the bottom of our form in the admin_page method:

<input type="submit" name="wp-gallery2-reset" value="'._('Reset Options').'" />

Now, when the user clicks on this button, the submitted form will have a field called “wp-gallery2-reset”, which we can look for.

The place where we’ll look for the presence of this field is the option validation callback. If you remember from the Building the Plugin’s Settings Page post, when we registered our option name with WordPress, we also passed in a callback that will be used to validate the values of the settings. Another purpose of this callback is to process the option reset request if one is present.

The callback itself looks like this:

  /**
   * validate_options
   * Validate the options the user has supplied or reset them
   *
   * @param array $options The array of options
   * @return array $options
   */
  public function validate_options ($options) {
    if (isset ($_POST["wp-gallery2-reset"])) {
      $this->set_default_options ();
      $_REQUEST["_wp_http_referer"] = add_query_arg ("defaults", "true",
						     $_REQUEST["_wp_http_referer"]);
      return $this->options;
    }
    return $options;
  }

As you can see, currently the callback’s only job is to process the reset request, it does not do any validation for now. The way that the callback is supposed to work is that the options which the user has supplied are passed into it. The callback processes the options and returns them back to WordPress. The values that it returns are the values stored in the database. Therefore, this is the perfect spot to reset any options to their default values (if requested).

As you can see, we check whether the $_POST superglobal contains an element called wp-gallery2-reset. If so, we set the options array to its default values and return it.

The purpose of the line

$_REQUEST["_wp_http_referer"] = add_query_arg ("defaults", "true",
						     $_REQUEST["_wp_http_referer"]);

is to add an argument to the request which can be used by the admin_page method to figure out whether a reset has been done. Then it can display a message to the user of that effect.

VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)