One button on admin page works but not two

I am workin on a plugin (for use on my own site). I recently added a button to the admin page that generates some text, and it works fine. This is what I use (pilfered from examples):

if (!current_user_can('manage_options'))  {
  wp_die( __('You do not have sufficient galooph to access this page.')    );
}

if ($_POST['plugin_button'] == 'thing' && check_admin_referer('thing_button_clicked')) {
  plugin_thing_button();
}
echo '<form action="options-general.php?page=plugin-list" method="post">';

wp_nonce_field('thing_button_clicked');
echo '<input type="hidden" value="thing" name="plugin_button" />';
submit_button('Generate new thing');

echo '</form>';

This works fine and calls the function as it should.

Now I want a second button to do something completely unrelated.

Here is what I tried, basically copying from above:

if (!current_user_can('manage_options'))  {
  wp_die( __('You do not have sufficient galooph to access this page.')    );
}

if ($_POST['plugin_button'] == 'thing' && check_admin_referer('thing_button_clicked')) {
  plugin_thing_button();
}
if ($_POST['plugin_button2'] == 'thing2' && check_admin_referer('thing2_button_clicked')) {
  plugin_thing2_button();
}
echo '<form action="options-general.php?page=plugin-list" method="post">';

wp_nonce_field('thing_button_clicked');
echo '<input type="hidden" value="thing" name="plugin_button" />';
submit_button('Generate new thing');

wp_nonce_field('thing2_button_clicked');
echo '<input type="hidden" value="thing2" name="plugin_button2" />';
submit_button('Generate new new thing');

echo '</form>';

The code for 2 buttons returns “The link you followed has expired.” for both buttons, i.e. the one that worked alone does not work either now.

Where is my mistake? Thank you in advance!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I think I’ve solved the problem, there are 2 issues:

  1. You have 1 form for both buttons, submitting means submitting the entire form, so both buttons are submitted
  2. Your nonce/referrer checks appear to be incomplete

The second item is why you’re having issues, although server time issues could also factor in.

If we inspect your nonce and check closer:


...

check_admin_referer('thing_button_clicked')

...

wp_nonce_field('thing_button_clicked');

Notice that they only use the first parameter which is the action. They do not specify the name. This means that when you use wp_nonce_field, it will default too _nonce, resulting in <input type="hidden" name="_nonce"... which is a problem when you have 2 nonces.

So specify the second parameter, or switch to a singular nonce

Method 2

Thanks to Tom J Nowell, I found the solution. The problem was putting both buttons in the same . This fixed it:

echo '<form action="options-general.php?page=plugin-list" method="post">';
wp_nonce_field('thing_button_clicked');
echo '<input type="hidden" value="thing" name="plugin_button" />';
submit_button('Generate new thing');
echo '</form>';

echo '<form action="options-general.php?page=plugin-list" method="post">';
wp_nonce_field('thing2_button_clicked');
echo '<input type="hidden" value="thing2" name="plugin_button2" />';
submit_button('Generate new new thing');
echo '</form>';

(I hope this is the right way to go about this! And thank you again, Tom!)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x