How to submit data from HTML form?

I agree with other answers that most of the time it is better to use a plugin to handle form submissions. However, there are exceptions to this rule.

If you find yourself in a situation where you need to handle the form submissions yourself, you can submit data to admin-post.php. Here is how to set that up:

First set up your form.

<!-- Submit the form to admin-post.php -->
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="POST">

    <!-- Your form fields go here -->

    <!-- Add a hidden form field with the name "action" and a unique value that you can use to handle the form submission  -->
    <input type="hidden" name="action" value="my_simple_form">

</form>

Now you will set up an action in your functions.php file. You will use the value that you used in your hidden action field in the form.

function my_handle_form_submit() {

    // This is where you will control your form after it is submitted, you have access to $_POST here.

}

// Use your hidden "action" field value when adding the actions
add_action( 'admin_post_nopriv_my_simple_form', 'my_handle_form_submit' );
add_action( 'admin_post_my_simple_form', 'my_handle_form_submit' );

Here is a link to more information from the codex – https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)

Leave a Comment