php - How to handle a custom form in wordpress to submit to another page?

admin2025-06-06  8

I know in PHP I can just put action equal to random.php file and process the data there, but how in wordpress can I use already existing page with a custom template to submit a form to, so that after submitting a form on one page, user will be redirected to another page in wordpress with all his inputted credentials still available?

I know in PHP I can just put action equal to random.php file and process the data there, but how in wordpress can I use already existing page with a custom template to submit a form to, so that after submitting a form on one page, user will be redirected to another page in wordpress with all his inputted credentials still available?

Share Improve this question asked Nov 12, 2018 at 22:25 LimpulsLimpuls 3071 gold badge3 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 6
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
<input type="hidden" name="action" value="your_action_name">

Add these in your form. Where admin-post.php will process your form. In that case based on the value of your_action_name that is provided by you, an action hook will get involved. Say for example if you add a hook like the following in functions.php of your theme or in your plugin

add_action( 'admin_post_nopriv_your_action_name', 'your_function_to_process_form' );

then for non logged in user

function your_function_to_process_form(){
// process your form here
}

will be called. From there you can process your form. For logged in user you need to rename your action to admin_post_your_action_name from admin_post_nopriv_your_action_name. Remember admin_post_ or admin_post_nopriv_ are available in admin-post.php to do_action appropriate action. Whatever you append at the end of admin_post_nopriv_ or admin_post_ will formulate a action hook. That needs to implemented by add_action(). If you pass contactform as a hidden action then your action hook will be either admin_post_nopriv_contactform or admin_post_contactform or both.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749187284a317094.html

最新回复(0)