7

I tried to use a form with the following markup using the action admin-post.php

<form method='post' action='admin-post.php'>

I'm using the action hook suggested by Wordpress to proceed the $_POST parameters and it works fine. However after execution I got a blank page and no redirect back to the plugins page. Of course I could set a custom redirect with the referer, but is that the normal behaviour?

A Workaround is to use action="" and and getting the request in my normal plugin code.

  • 3
    If you use admin-post.php and admin_post action you need to redirect back to your plugin page. – cybmeta Feb 4 '15 at 18:15
  • That information should be added to the codex. I wasn't sure about this. – Rubberducker Feb 5 '15 at 8:46
  • 1
    If you send the form to admin-post.php, admin.post.php is where the user go. Just see the content of that file to know that it doesn't print anything, just check the authentication cookie and run the functions hooked to admin_init first and admin_post later. – cybmeta Feb 5 '15 at 8:59
  • 1
    wp_redirect does not work when posting forms with admin-post. It will give error that headers are already sent, so this seems to be a worthless method for posting a form and trying to redirect somewhere afterwards. – Solomon Closson Dec 2 '16 at 7:26
16

Maybe a little late but I stumbled on this when I was having issues figuring it out so thought I would supply what I found out for future people.

I've found the basic principles are to have a hidden input named action and it's value being a custom set identifier. For example

<input name='action' type="hidden" value='custom_form_submit'>

With this input, and your forms action pointing to admin-post.php an action can be set. We set this action using admin_post_custom_form_submit.

To make things more complicated we can use a wp_nonce_field which I think is a basic security thingy. Basically it adds a random $_POST value. Fair enough.

Next we want to set our action like so:

add_action('admin_post_custom_form_submit','our_custom_form_function');

So when there is a form submitted to admin-post.php and there is an action value of custom_form_submit the function our_custom_form_function will be called! :D

function our_custom_form_function(){
    //print_r($_POST);
    //you can access $_POST, $GET and $_REQUEST values here. 
    wp_redirect(admin_url('admin.php?page=your_custom_page_where_form_is'));
   //apparently when finished, die(); is required. 
}

Now you say you get a white page. This is because we need to redirect back to our form. I've used a simple wp_redirect()

Hope this has helped :) I'm going to try and figure out how I can do some validation and give errors back to our redirect form. I think the simplest idea would be to make a $_GET value and find that on our page but it's not great is it?

I've also found that once submitted $_POST is cleared!! DX This is probably to do with the redirect. I'll have a google and see what I can find :d

Hope this has helped :)

UPDATE

I done some more work and realised the only real way to return values is to use the $_GET variable. This way you can re-enter any post values. Just don't forget to use urlencode()to ensure that special characters such as '@' and so on are included.

I had more than one page with what I was working on so I done 2 different redirects to the different pages and included the errors and so on. The checked for them above my form.

Another handy function. http_build_request() can turn arrays into 'url safe' arrays so that you can send them over $_GET requests etc.

  • Hi bro thanks this is working for me..! but one problem is there, how can i set error to admin.php?page=your_custom_page_where_form_is i did used add_settings_error() for display error but it not display any error because page is redirect..! can you please help me for that..? thank – Bhavik Hirani Sep 2 '17 at 6:23
  • Hi @BhavikHirani I don't really use wordpress anymore but my guess would be you need to do your validation before the wp_redirect function call and have an if statement to say that your validation was passed before redirecting. Hope this helps? – sourRaspberri Sep 4 '17 at 8:12
  • after form submission i want to show success message in 'admin.php?page=your_custom_page_where_form_is' how is it possible? – Rijo Mar 24 '18 at 2:48
  • @Rijo tried echo? – sourRaspberri Mar 26 '18 at 8:54
3

Before redirecting you can save any validation errors and post values into a transient. These values will persist after the url redirection.

This is exactly how WP core does it for the Settings API when returning errors.

So:

-validate form data

-store post data in transient with set_transient()

-store any errors in transient

-redirect to form output

-check for errors in transient

-load post values into form fields from transient

You could use a session or the Options API instead of a transient.

Now, I've been experimenting with various ways of handling form submissions both on the frontend and on the admin. I'm not sure admin_post_{action} is all that convenient because of the need to persist data and errors across the redirect. Using the init or admin_init hook to check for a form submission might be easier. You could also simply post your form back to the same page and process the form submission before the form is displayed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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