I have the following form in HTML on the front-end of a WordPress website which I am trying to convert from a website that is currently built in NodeJS.
<form id="regForm" action="/submit-form" method="POST">
<label for="name">Name</label>
<input type="text" id="name" name="name" placeholder="Enter your name">
<label for="email">Email address</label>
<input type="email" id="email" name="email" placeholder="Enter your email address">
<input type="submit" class="submit-button" value="Submit">
</form>
How do I build a handler for the POST request in WordPress? The below is my current attempt but this redirects to my Error 404 page. It is in my function.php file.
function form_submission() {
// Check if form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Sanitize input
$name = sanitize_text_field($_POST['name']);
$email = sanitize_email($_POST['email']);
// Perform actions, e.g., send an email or store data
//wp_mail(get_option('admin_email'), 'New Form Submission', "Name: $name\nEmail: $email\nMessage: $message");
// Redirect after processing
wp_redirect(home_url('/verify-email'));
exit;
}
}
add_action('form-submission', 'form_submission');
I have the following form in HTML on the front-end of a WordPress website which I am trying to convert from a website that is currently built in NodeJS.
<form id="regForm" action="/submit-form" method="POST">
<label for="name">Name</label>
<input type="text" id="name" name="name" placeholder="Enter your name">
<label for="email">Email address</label>
<input type="email" id="email" name="email" placeholder="Enter your email address">
<input type="submit" class="submit-button" value="Submit">
</form>
How do I build a handler for the POST request in WordPress? The below is my current attempt but this redirects to my Error 404 page. It is in my function.php file.
function form_submission() {
// Check if form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Sanitize input
$name = sanitize_text_field($_POST['name']);
$email = sanitize_email($_POST['email']);
// Perform actions, e.g., send an email or store data
//wp_mail(get_option('admin_email'), 'New Form Submission', "Name: $name\nEmail: $email\nMessage: $message");
// Redirect after processing
wp_redirect(home_url('/verify-email'));
exit;
}
}
add_action('form-submission', 'form_submission');
Share
Improve this question
asked Feb 15 at 13:45
Eind999Eind999
101
1
|
1 Answer
Reset to default 0you can use admin-post.php
to process the form.
you can set the form like that :
<form
action="<?= htmlspecialchars(admin_url("admin-post.php?action=MY_PLUGIN__my_action"))?>"
method="POST"
>
<button name="send">go</button>
</form>
and then you link the code with that :
add_action("admin_init", function () {
if ( !isset($_GET["action"])
|| ("MY_PLUGIN__my_action" !== $_GET["action"])
) {
return;
}
/* */
// debug
var_export($_POST);
exit();
/* */
// data processing
//...
// redirection at the end
wp_redirection(home_url("done"));
exit();
});
name
andemail
are used internally already and will always lead to a 404. – fuxia ♦ Commented Feb 15 at 17:58