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

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