I created my first WordPress plugin (I have a lot of coding experience, but that comes from the embedded software world).
It includes a form, presented on the front end.
To process the form entries, I added an action that addresses admin-post.php:
<form action="<?php echo esc_attr( admin_url( 'admin-post.php' ) ); ?>" method="post">
I added hooks for logged-in and non-logged-in users to evaluate the submitted form.
add_action( 'admin_post_no1_remember_form_response', array( $this, 'no1_evaluate_remember_form' ) );
add_action( 'admin_post_nopriv_no1_remember_form_response', array( $this, 'no1_evaluate_remember_form' ) );
After doing some checking, securing and processing of the form-data, I want to go back to the page where the form was, but showing some success message instead of the form. That works fine but I can not figure out (though I googled a lot), how to redirect to the page where the form was submitted.
In my dev-environemnt I used the wp_redirect function with a hard coded path:
wp_redirect(
esc_url_raw(
add_query_arg(
array(
'info' => $form_eva_result,
'response' => $_POST,
),
home_url( 'index.php/test-shortcode-no1/' )
)
)
);
exit;
That works fine, but since I want to place the form (via a shortcode) on any page I need to find a way to retrieve the correct URL for the page where the form was submitted.
I understand that using AJAX to submit the form data might be an option, but for now I would like to stick with the POST method (also for educational purposes 🙂 )
Can any one suggest a way to retrieve the correct redirect target?
Thanks, Christian