Wordpress. Get data from admin-post.php after form validation failed
I submit the form to admin-post.php:
<form action="<?php echo admin_url('admin-post.php') ?>" method="post">
<input type="hidden" name="action" value="some-action">
<input type="text" name="somename">
</form>
And process it in the backend:
add_action( 'admin_post_some-action', function () {
$isValid = // SOME VALIDATION HERE
if($isValid){
wp_redirect('next-step'); //redirect to the next form
return;
}
wp_redirect('current-step'); //redirect back
});
If the form is invalid, I want to redirect back to the form with filled data and highlight invalid inputs. The problem is wp_redirect doesn't return any data at all. Thus, the user will have to manually fill in all the data without even realizing that the wjat was wrong. I found 2 solutions:
- Use get param in wp_redirect - this might only be useful for one login, but it will be terrible, especially for entering files and passwords.
- Using $ _SESSION is a slightly better, but still crude way to implement it.
- Using curl is a very messy WP solution
I guess there should be an easy default way to deal with this, since it's pretty important. Any ideas?
source to share
Use transients if the user is logged in and set their expiration to around 60 seconds, and set the data in your admin_post action and then extract it on the form page. I'm still looking for a nice solution that doesn't involve submitting the form back to itself by setting it action=""
to the form and not using an action admin_post
.
source to share
"Simple" or "hard" programming, instructions must be provided to the machine. Think how you would do it with simple PHP. After a failed login, you are redirected to another page. Since HTTP is statusless, you can either store data on the server side (in a database, session, files ..) or pass the information in the url. WordPress won't just automatically start a session to store the last form.
source to share