How do I handle multiple Submit buttons in plugin’s option page?

I’m developing my first plugin for WordPress and I’m getting a grasp of the whole hook system. I’m now stuck on a (probably) small issue, that I could possibly circumvent, but I’m afraid of coming out with a “hack”, more than a real solution.

What I’m trying to do is putting two submit buttons on my Options page:

  • Save
  • Draft

Both of them would have to do something common (i.e. Saving the content of the form), but then they should take different action with such content. So far, I managed to ouput the two buttons using the following code:

echo '<form id="my_plugin_settings_form" method="post" action="options.php">';
settings_fields('my_plugin_settings');
do_settings_sections('my_plugin_settings');
submit_button(__('Save', 'MyPlugin'), 'Save');
submit_button(__('Draft', 'MyPlugin'), 'secondary','Draft');
echo '</form>';

I also implemented a validate method, which correctly receives the posted fields:

public function validate_settings($settings) {
  var_dump($settings); // Here I can see all posted fields
}

The above works well, with all the data being saved correctly in WordPress Options table. However, I would like to “intercept” the data being saved to manipulate it. I was thinking of manipulating the data within the validate_settings() method, but the button that was clicked is not included in the settings passed as an argument.

I could simply parse the $_POST array to get it, but I’m having the doubt that the approach is a bit of a hack and, that, perhaps, I should implement a proper hook, somewhere else.

My question, therefore, is: where should I put my code to process the data posted from an Options page? Is there a specific hook I should use?

Thanks in advance for the answers.

1 Answer
1

The settings API is crap. I need a long time and need try many until I got this working. My problem was that the values from my inputs never was passed to the validate callback.

I end up in using the array method:

<input type="text" name="foo[bar]" value="baz" />

If the formular will be send, in the $_POST or $_GET array is a field called foo that contains an array with all values. So I can access the value with e.g. $_POST['foo']['bar']

If I had to work with the settings API, I use the second parameter fomr register_setting() (the option name) as key for my input fields.

global $option_name;
$option_group = 'The_Option_Group';
$option_name="The_Option_Name";
register_setting( $option_group, $option_name );
[...]

function field_callback() {
  global $option_name;
  echo "<input type="text" name="{$option_name}[bar]" value="baz" />";
  echo "<input type="submit" name="{$option_name}[submit]" value="Send" />"
}

In this way I can access all values within the formular in the option validate callback.

function option_validate( $input ) {
  $bar = $input['bar'];
  $sub = $input['submit'];

  return $input;
}

It’s better explained in the article Settings API Explained on PressCoders

Leave a Comment