如何通过wordpress主题正确实现基本的联系表单?

时间:2019-06-08 06:46:30

标签: wordpress wordpress-theming contact-form

我为主题beat-mix-lite创建了一个wordpress儿童主题。在那里,我创建了一个contact-form.php文件,其中包含模板和一个具有发送电子邮件功能的mail.php(在另一个文件夹中)。

我尝试不使用wordpress发送它,并且在本地完全正常。但是,使用wordpress时,无论表单本身如何操作,我总是会收到404错误。

我在提交表单时在其中尝试了vardump()时调用了functon sendmail()。

<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-load.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/wordpress/wp-content/themes/custom-child/functions/mail.php');
?>
<form action="<?php echo sendMail(); ?>" method="post">
                    <label for="name">
                        <?php echo getTranslatedText('Name'); ?>: <span>*</span>
                    </label>
                    <input type="text" name="name" placeholder="<?php echo getTranslatedText('NameExample'); ?>: ">
                    <label for="email">
                        <?php echo getTranslatedText('Email'); ?>: <span>*</span>
                    </label>
                    <input type="text" name="email" placeholder="<?php echo getTranslatedText('EmailExample'); ?>: ">
                    <label for="textarea">
                        <?php echo getTranslatedText('MessageExample'); ?>: <span>*</span>
                    </label>
                    <textarea name="textarea" rows="8" placeholder="<?php echo getTranslatedText('Matter'); ?>: ...">
                        <?php echo esc_textarea($_POST['textarea']); ?>
                    </textarea>
                    <input type="submit" name="submit" value="<?php echo getTranslatedText('Submit'); ?> " type="reset" value="Clear">
                </form>

// function to send the mail
<?php
function sendMail() {
    if(isset($_POST['email'])) {

        // EDIT THE 2 LINES BELOW AS REQUIRED
        $email_to = "myemail@domain.com";
        $email_subject = "Your email subject line";

        function died($error) {
            // your error code can go here
            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }

        // validation expected data exists
        if(!isset($_POST['name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['textarea'])) {
            died('We are sorry, but there appears to be a problem with the form you submitted.');
        }
        $name = $_POST['name']; // required
        $email_from = $_POST['email']; // required
        $textarea = $_POST['textarea']; // required

        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

        if(!preg_match($email_exp,$email_from)) {
            $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
        }
        $string_exp = "/^[A-Za-z .'-]+$/";

        if(!preg_match($string_exp,name)) {
            $error_message .= 'The Name you entered does not appear to be valid.<br />';
        }
        if(strlen($textarea) < 2) {
            $error_message .= 'The Comments you entered do not appear to be valid.<br />';
        }
        if(strlen($error_message) > 0) {
            died($error_message);
        }

        $email_message = "Form details below.\n\n";

        function clean_string($string) {
            $bad = array("content-type","bcc:","to:","cc:","href");
            return str_replace($bad,"",$string);
        }

        $email_message .= "Name: ".clean_string($first_name)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Nachricht: ".clean_string($textarea)."\n";

        // create email headers
        $headers = 'Von: '.$email_from."\r\n".
        'Antworten an: '.$email_from."\r\n" .
        'X-Mailer: PHP/' . phpversion();
        mail($email_to, $email_subject, $email_message, $headers);

        echo "Thank you for contacting us. We will be in touch with you very soon.";
    }
}

我希望稍后到达同一页面并显示一条成功消息,并将电子邮件发送到我的地址(在代码中进行了更改)。我什至问问都很愚蠢,希望能提供很多帮助。 :)

可以下载父主题here

1 个答案:

答案 0 :(得分:0)

我认为您的操作不适用于wordpress永久链接结构。 尝试将此功能用于表单操作:

 action="<?php echo get_the_permalink(); ?>"

这会将您的表单指向当前站点。

然后像这样调用您的邮件功能:

<?php if(isset($_POST['email']) && isset($_POST['name']) && isset($_POST['MessageExample'])){
 sendMail();
 } ?>