Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

5 Answers 5

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.

share|improve this answer
    
Thanks, I wanted something different. My bad, I have formulated it badly. Please, check my updated code in my question to see what I mean. Thanks. –  Derfder May 20 '13 at 12:59
    
@Derfder Try my suggestion. It does what you are asking for. –  toscho May 20 '13 at 13:00
    
So, when I click on the submit button, instead of reloading current plugin page I am redirected somewhere else. So, I need after the click run through my code and check the GET condition and in my case it will be true and the code will be executed, because I have clicked on submit and do_it_hidden is sent. In standard php I would do it like action="the_same_file_as_i_am_in" or maybe even action="". What to put in action="???" so it works in WP plugin admin page? –  Derfder May 20 '13 at 13:03
    
The action should be admin_url( "options.php" ) or admin_url( "admin-post.php" ). Then register your form handler for that action, do the work, and redirect. –  toscho May 20 '13 at 13:07
    
"Try my suggestion. It does what you are asking for." 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 –  Derfder May 20 '13 at 13:07
<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' ) );
share|improve this answer
    
hmm, I have updated my post with additional info. My file where is located the code is called DD_awesome_plugin.php –  Derfder May 20 '13 at 13:10
    
In php it's so simple to trigger get forms. Is there some tutorial for WP? I don't get it at all. I just want to execute different parts of my spaghetti code depending on which form submit is pressed. –  Derfder May 20 '13 at 13:15

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

share|improve this answer
    
You can use a GET request however by using <form method="get" action="<?php echo admin_url('DD_Awesome_Plugin/DD_awesome_plugin.php'); ?>" enctype="multipart/form-data">, Sounds like your ok with posting though but just wanted to give you some FYI lol, I tested this on my box and it works as intended so it's not just a hunch. –  meekbot May 20 '13 at 13:30
    
Because you are using enctype="multipart/form-data" you should be using POST anyway. But yes, you do need to provide the correct and complete URI, which you were not doing. That is not WordPress' fault. It is the way that your browser and HTML/HTTP work. –  s_ha_dum May 20 '13 at 13:44
    
Yes thank you for that correction, sorry copy and paste got away from me >.<, with a get request you would remove the enctype="" attribute. –  meekbot May 20 '13 at 13:52
    
Nop, it's not hard to do simple thing in WP, just test my answer. –  Shazzad Dec 12 '13 at 19:48
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>
}
share|improve this answer

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>
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.