doqw89029 2018-08-04 11:21
浏览 77
已采纳

WordPress表单操作错误

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:

  • Setting WP_DEBUG within wp-config.php to true (I got zero errors); and
  • I created an "action" folder with a "newsletter.php" file inside it, and changed the action on my form to /action/newsletter.php, but again, I just got the blank web page (this approach worked fine when I tested it on a static website, one without WordPress).

Also, in case it is of relevance, here is a brief overview of the folder structure:

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

  • 写回答

1条回答 默认 最新

  • dsy48837 2018-08-08 18:52
    关注

    I managed to find a solution to my question in this article, Build your own WordPress Contact Form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题