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

<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.

Leave a Comment