How to trigger $_GET request within admin plugin page?

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:

Read More
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

Related posts

5 comments

  1. 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 😉

  2. This should work

    if (isset($_GET['action']) && 'do_it_hidden' == $_GET['action'] ) {
       // some code to execute here
    } else {
       // just show the form
       <form method="get" action="options-general.php">
         <p class="submit">
           <input type="hidden" name="action" value="do_it_hidden">
           <input type="hidden" name="page" value="<?php echo wp_unslash( $_REQUEST['page'] ); ?>">
           <input name="do_it" type="submit" class="button-primary" value="DO IT!"> 
         </p>
       </form>
    }
    
  3. Use a redirect in your form handler:

    if (isset($_GET['do_it_hidden'])) {
        // some code to execute here
    
        wp_redirect( admin_url( "options-general.php" ), 303 );
        exit;
    }
    

    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.

  4. <form method="get" action="do_it.php" enctype="multipart/form-data">
    

    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

    <form method="post" action="options.php" enctype="multipart/form-data">
    

    It uses that file to process the post information then redirects the user to the display page like so.

    wp_redirect( admin_url( 'options-general.php?updated=true' ) );
    
  5. if (isset($_GET['do_it']) && $_GET['action'] =='do_it_hidden') {
       // some code to execute here
    } else {
       // just show the form
       <form method="get" action="general.php?page=<?php echo $_REQUEST['page']?>" enctype="multipart/form-data">
         <p class="submit">
           <input type="hidden" action="do_it_hidden" value="run">
           <input name="do_it" type="submit" class="button-primary" value="DO IT!"> 
         </p>
       </form>
    }
    

Comments are closed.