If I submit a form with an action of /wp-admin/admin-post.php
, is there a way to return form errors to the originating page?
My form has an input with the action set:
<input name="action" value="enquiry_form" />
I have an actions setup to manage the validation:
add_action('admin_post_enquiry_form', 'mytheme_enquiry_form_submit');
I can return to the original form page with a wp_redirect:
wp_redirect('/contact/');
But how can I pass variables/ messages back to this page after redirecting?
UPDATE: To add clarification, I was under the impression this was the "correct" way to handle form data, rather than simply adding an init hoot to check for a specific POST variable. There are countless tutorials where people use the admin-post.php action, for example:
WPMUDEV Handling Form Submissions
admin_post
andadmin_post_nopriv
actions if not for logged-in and logged-out users? I thought forms should always use this so that actions could be setup correctly? I can easily (and securely) handle the field validation, and am fine handling POST data if the form submits back to itself. I was under the impression using my initial method was the "right" way to do things?admin-post.php
doesn't do what I thought it did, eitherway it's rare you'll see it in plugins, and a lot of security restrictions that can be made don't account for it at all. Usually the page that renders the form also handles it, so you leave theaction
empty, this way all your form code is in one place. Also look into the REST API instead of using Admin AJAX