$_POST form request with admin-post

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! 😀

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.

Leave a Comment