How to add a secondary button to a settings page with a custom action?

I have written a plugin that interfaces with a third-party mailing API. I have created a settings page for my plugin that includes two buttons:

  • a submit button created with submit_button() which saves the form
  • a “test settings” button that should use the current values from the form without saving them

This is what I currently have:

echo '<form method="post" action="options.php">';
settings_fields('myplugin_settings');
do_settings_sections('myplugin');
echo '<p>';
submit_button('Test Settings', '', 'test', false);
echo ' ';
submit_button(null, 'primary', 'submit', false);
echo '</p></form>';

This produces two buttons, as expected:

enter image description here

However, I don’t know how to override the action for the “test settings” button. Is there a hook for this? I can’t find any documentation for this.

1 Answer
1

It’s not clear to me what the “test” button is for, which I guess could make a difference, but, in general, I have found that to make two buttons work independently on the same options page, the easiest thing is to place them within two separately defined forms, even if the two buttons are going to be placed side by side.

The simplest method is just to create a “mini-form” for the “special” button, and, if need be use a little CSS to place the second button before or above, etc., the “primary” button. So, depending on what else is going on where you are placing the button, if you really need and want the test button to appear first, you could float it to the left. Also, you should note that, though you can style the button produced by “submit_button” using optional parameters (“type,” “attribute” – see Codex), you aren’t obligated to use it with the Settings API: You can produce the same button, and then add whatever selectors or element-styles, with the regular old button html that submit_button() produces.

So, mainly to illustrate:

echo '<form method="post" action="options.php">';

settings_fields('myplugin_settings');

do_settings_sections('myplugin');

echo '<p>';

submit_button(null, 'primary', 'submit', false);

echo '</form>';

echo '
    <form method="post" name="test-button">

         <span id="test-button">

              <input id="test-settings" type="submit" value="Test Settings" class="button" >

         </span>

    </form>
';

Leave a Comment