Here I’ve added a this hidden input <input type="hidden" name="action" value="your_form_task">
and updated the post URI to <?php echo esc_url( admin_url('admin-post.php') ); ?>
.
Here is the updated form-
<form method="get" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" name="emailForm" onsubmit="return validate()">
<div class="form-group">
<label for="name">Name<span id="req">*</span></label><br>
<input type="text" class="form-control" id="name" name="name" placeholder="Quantum Management" value="">
</div>
<div class="form-group">
<label for="tel">Phone Number<span id="reqTel">*</span></label><br>
<input type="text" class="form-control" id="tel" name="tel" placeholder="(999) 123-4567" value="" >
</div>
<div class="form-group">
<label for="email">Email<span id="reqEmail">*</span></label><br>
<input type="text" class="form-control" id="email" name="email" placeholder="[email protected]" value="" >
</div>
<div class="form-group">
<label for="questions">Questions<span id="reqQuest">*</span></label><br>
<textarea class="form-control" rows="7" cols="8" id="questions" name="questions" placeholder="Questions" ></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default" name="submit" value="yes">Submit</button>
</div>
<input type="hidden" name="action" value="your_form_task">
</form>
Note that it is very important. Now here is the function where you can put your logics to handle form data-
function the_dramatist_form_post_to_admin() {
/**
* At this point, $_GET/$_POST variable are available
*
* We can do our normal processing here
*/
// Sanitize the POST field
// Generate email content
// Send to appropriate email
// Or do whatever you want to do.
}
add_action( 'admin_post_nopriv_your_form_task', 'the_dramatist_form_post_to_admin' );
add_action( 'admin_post_your_form_task', 'the_dramatist_form_post_to_admin' );
Look here is another important thing, after admin_post_nopriv_
and admin_post_
I’ve glued the your_form_task
string what we’ve declared as hidden input value in the form with input name action
. It’s actually pointing the form to be submitted in this function. And admin_post_nopriv_{$action}
fires on a non-authenticated admin post request for the given action where admin_post_{$action}
fires on an authenticated admin post request for the given action. Here $action
is your_form_task
. Hope that helps.