Products
GG网络技术分享 2025-03-18 16:12 2
I have custom coded a form in WordPress, and I am using PHP to handle the submission.
I followed this tutorial, Handling POST Requests the WordPress Way, but on submit I am presented with a blank web page.
Here is my HTML code:
<form method=\"post\" action=\"<?php echo esc_url(admin_url(\'admin-post.php\')); ?>\"><input type=\"hidden\" name=\"action\" value=\"newsletter_signup\"/>
<label for=\"newsletterEmail\">Email:</label>
<input type=\"email\" name=\"newsletterEmail\" required/>
<button type=\"submit\" name=\"newsletterSubmit\" value=\"Submit\">
</form>
Here is my PHP code:
function m_newsletter_signup() {if (!empty($_POST)) {
$newsletterEmail = $_POST[\'newsletterEmail\'];
$to = \'contact@example.com\';
$subject = \'Newsletter Signup\';
$body = \"Email: $newsletterEmail\";
$from = \'Website\';
if ($_POST[\'newsletterSubmit\']) {
if (mail ($to, $subject, $body, $from)) {
header(\"Location: https://example.com/site/newsletter-success/\");
exit();
} else {
header(\"Location: https://example.com/site/newsletter-error/\");
exit();
}
}
}
}
add_action(\'admin_post_nopriv_newsletter_signup\', \'m_newsletter_signup\');
add_action(\'admin_post_newsletter_signup\', \'m_newsletter_signup\');
My form is contained within a .php file within an \"inc\" folder, and my PHP is contained within functions.php.
I have tried the following:
Also, in case it is of relevance, here is a brief overview of the folder structure:
themeaction
newsletter.php
assets
inc
form.php
functions.php
Any advice would be greatly appreciated.
Thanks,
Matthew
P.S. I do not wish to use a plugin as an alternative.
I managed to find a solution to my question in this article, Build your own WordPress Contact Form.
Demand feedback