PHP联系表格不适用于Wordpress

汽车

我使用mail()构建了一个简单的php联系表单。该表格已经在我的网站上使用了多年。

但是,我最近在wordpress上建立了一个新网站。我几乎复制并粘贴了相同的表格,但是,我没有收到任何电子邮件。当我单击“提交”时,该表单仅重定向到主页/索引布局,但是具有联系表单的网址。

但是,在原始站点上,空白屏幕上会显示一条消息,确认已发送电子邮件。我还在原始站点上测试了原始代码,并且可以正常工作。

contact_form.php:

 <?php
    /*
        Template Name: Contact Form
   `*/

    include( 'header.php');
?>

<?php

    if(have_posts()): while (have_posts()): the_post();
?>


<div class="wrapper">
    <div class="message">

      <div class="text">
        <?php the_content(); ?>
        <form action="contact_form_script.php" method="post">
            <p>Your email address:</p> <input type="text" name="emailAddress">
            <p>Subject:</p> <input type="text" name="subject"> 
            <p>Message:</p> <textarea name="message"></textarea>

            <input type="text" name="honeyPot" style="display: none;">

            <br />
            <input type="submit" name="email_form" value="SUBMIT"/>
        </form>

        <br />
        <br />


      </div>
    </div>
</div>

<?php
    endwhile;
    endif;
?>

<?php
    include('footer.php');
?>

contact_form_script.php

<?php

    $to= '[email protected]';

    $from = "FROM: [email protected]"; 

    $replyTo=$_POST['emailAddress']; /*whatever the user input*/

    $headers= $from ."\r\n" .'Reply-To: '. $replyTo; 

    //Have also tried just $_POST['subject']; Still doesn't work
    $subj="Subject: "+$_POST['subject'];

    $msg=$_POST['message'];

    $spam=$_POST['honeyPot'];

    if (empty($spam)){
        mail($to, $subj, $msg, $headers);
    };

    Print "Thank you for email! <br /> <br /> <a href='/'><--BACK</a>"


?>

另外,除了检查我是否收到电子邮件以外,还有没有更好的方法来测试这种形式的表格以查看错误或进行故障排除?

利·比克内尔

WordPress中有许多方法可以完成您要完成的工作。

当前,您的表单操作所指向的位置contact_form_script.php假定该脚本位于当前目录中。但是该当前目录与您的目录无关contact_form.php它相对于用户当前路径。例如www.mysite.com/contact/。

解决问题的一种快速(但懒惰)的方法是移至contact_form_script.php您网站的根WordPress目录。然后更改form action="/contact_form_script.php"(请注意/),使它发布到contact_form_script.php根站点目录中称为的文件中。

更好的方法是保留contact_form_script.php在模板目录中,并将其包含在functions.php中。

如果我们使用email_formSubmit输入让WordPress知道contact_form_script.php需要加载脚本,则这应该允许您加载整个wordpress核心,并且仍然让您的自定义表单处理脚本处理数据:

functions.php:

if (!empty($_POST['email_form'])) {
    require_once(__DIR__.'/contact_form_script.php');
    // You could put a die() here if you wanted the script to stop executing.
}

contact_form.php:

include( 'header.php');

if(have_posts()): while (have_posts()): the_post();


<div class="wrapper">
    <div class="message">

      <div class="text">
        <?php the_content(); ?>
        <form action="" method="post"> <!-- Notice we are now submitting our data to wordpress and not directly to our form script -->
            <p>Your email address:</p> <input type="text" name="emailAddress">
            <p>Subject:</p> <input type="text" name="subject"> 
            <p>Message:</p> <textarea name="message"></textarea>

            <input type="text" name="honeyPot" style="display: none;">

            <br />
            <input type="submit" name="email_form" value="SUBMIT"/>
        </form>

        <br />
        <br />


      </div>
    </div>
</div>

同样,这也不理想,但是我怀疑您目前正在寻找什么,这是朝着正确方向迈出的一步。将代码保留在模板中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章