Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Getting what you need out of $_POST is fine (as long as you validate and sanitize properly), that's how forms work after all. There probably is a way to wrangle button data into Settings API flow, but it is a mess and there will be little practical difference to what is happening. –  Rarst May 12 '13 at 14:03
add comment

1 Answer

up vote 2 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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