I have this form in my plugin function:
<form method="get" action="" enctype="multipart/form-data">
<p class="submit">
<input type="hidden" name="do_it_hidden" value="run">
<input name="do_it" type="submit" class="button-primary" value="DO IT!">
</p>
</form>
And this php condition earlier in my code:
if (isset($_GET['do_it_hidden'])) {
// some code to execute here
} else {
// just show the form
<form method="get" action="" enctype="multipart/form-data">
<p class="submit">
<input type="hidden" name="do_it_hidden" value="run">
<input name="do_it" type="submit" class="button-primary" value="DO IT!">
</p>
</form>
}
But after clicking on the button I am redirected to wp-admin/options-general.php?do_it_hidden=run&do_it=DO+IT
which is not what I want to happen.
After the click I just want to reload the page and check if the get condition is true or not.
How to do that?
EDIT:
The problem is that after I click on the button I am redirected to completely different page. My plugin is located here: options-general.php?page=DD_Awesome_Plugin/DD_awesome_plugin.php
but clicking on the button throws me here: options-general.php?do_it_hidden=run&do_it=DO+IT
SOLVED! I find that GET is not working at all! You need to use POST and in the form action you need to type this:
action="<?php admin_url('options-general.php?page=DD_Awesome_Plugin/DD_awesome_plugin.php'); ?>"
Damn! It’s sometimes so hard to do simple task in WP 😉
This should work
Use a redirect in your form handler:
The user will be redirected to a clean URL then and barely notice the short URL change. Or use a POST request to avoid a changed URL completely.
In the action part of your form you should include the php file that is responsible for processing that hidden form variable, when the form is submitted it will pass that information to the file you specify. Left blank it will post back to the current page you are on which is not what you want.
For instance the general options form gets directed to the options.php in the admin like so
It uses that file to process the post information then redirects the user to the display page like so.