0

I'm working on my settings page, I want to add two different buttons to the form and do something different depending which the user clicked.

The documentation states I can create two buttons like this:

submit_button('Submit', 'primary large', 'primary', false);
submit_button( 'Delete', 'delete', 'delete', false );

However I don't understand how to differenciate when the user clicked each one, it seems that no matter which button clicked the form is submitted the same.

How can I do something different depending what button the user clicks?

0

The submit_button() function is a wrapper for get_submit_button(). Now that function has multiple arguments, but the most interesting for your actual problem is the 3rd argument name. It sets the HTML name attribute.

<form action="">
    <input ... etc.
    <button type="submit" name="choice-a">
</form>

Now everytime you process your form with an empty action attribute, you will point to the current request http://example.com/wp-admin/example.php. There you will be able to fetch your arguments via the super globals $_POST or $_REQUEST (which simplified is mostly a combination of $_GET and $_POST).

Then just inspect the array and do whatever you need/want to do.

  • hi @kaiser I could get the $_POST on the page by removing the action attribute, however I just tried it and the form stops working since it doesn't go to options.php. Is there a solution for that? – Lisandro Vaccaro Jan 22 '15 at 16:26
  • use <form action="/options.php" type="post"> – Bysander Jan 23 '15 at 14:18
  • @LisandroVaccaro You maybe should update the code in your question and show your full example and what you really are trying to do. Hint: There's the "Settings API" built in. – kaiser Jan 23 '15 at 15:27
  • @Bysander Use esc_url( admin_url( 'options.php' ) ); instead :) – kaiser Jan 23 '15 at 15:27
  • Hey @kaiser - oh OK - what's the difference to using that - i use / all the time to make it easy merging between local & online code – Bysander Jan 27 '15 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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