admin管理员组

文章数量:1122832

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:

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.

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:

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.

Share Improve this question asked Jul 19, 2016 at 1:10 George EdisonGeorge Edison 4251 gold badge5 silver badges9 bronze badges 3
  • 1 Just use different formaction attributes on the buttons. – fuxia Commented Jul 19, 2016 at 2:06
  • @toscho - interesting - wasn't familiar with "formaction" attribute, but hard to find documentation on how specifically to use it in a case where the second button is also submitting a variable for action of the same type (i.e., submitting a discrete POST variable from same page) – CK MacLeod Commented Jul 20, 2016 at 20:56
  • I had the same problem, then I found the following guide. Hopefully, this will help you as well: https://www.sitepoint.com/handling-post-requests-the-wordpress-way/ – egillanton Commented Nov 2, 2020 at 14:11
Add a comment  | 

1 Answer 1

Reset to default 0

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>
';

本文标签: plugin developmentHow to add a secondary button to a settings page with a custom action