在front-page.php中导入联系表单

时间:2017-10-16 17:49:10

标签: php wordpress plugins

大家好,我正在尝试制作自定义联系表单,但我遇到了一些问题。 我在主题文件中创建了page-contact-us.php,并在CMS中创建了“联系我们”页面。 这是代码:

<?php
//response generation function

$response = "";

//function to generate response
function my_contact_form_generate_response($type, $message) {

    global $response;

    if ($type == "success")
        $response = "<div class='success'>{$message}</div>";
    else
        $response = "<div class='error'>{$message}</div>";
}

//response messages
$not_human = "Human verification incorrect.";
$missing_content = "Please supply all information.";
$email_invalid = "Email Address Invalid.";
$message_unsent = "Message was not sent. Try Again.";
$message_sent = "Thanks! Your message has been sent.";

//user posted variables
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$message = $_POST['message_text'];
$human = $_POST['message_human'];

//php mailer variables
$to = get_option('admin_email');
$subject = "Someone sent a message from " . get_bloginfo('name');
$headers = 'From: ' . $email . "\r\n" .
        'Reply-To: ' . $email . "\r\n";

if (!$human == 0) {
    if ($human != 2)
        my_contact_form_generate_response("error", $not_human); //not human!
    else {

        //validate email
        if (!filter_var($email, FILTER_VALIDATE_EMAIL))
            my_contact_form_generate_response("error", $email_invalid);
        else { //email is valid
            //validate presence of name and message
            if (empty($name) || empty($message)) {
                my_contact_form_generate_response("error", $missing_content);
            } else { //ready to go!
                $sent = wp_mail($to, $subject, strip_tags($message), $headers);
                if ($sent)
                    my_contact_form_generate_response("success", $message_sent); //message sent!
                else
                    my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
            }
        }
    }
}
else if ($_POST['submitted'])
    my_contact_form_generate_response("error", $missing_content);
?>



<div id="respond">
    <?php echo $response; ?>
    <form action="<?php the_permalink(); ?>" method="post">
        <div class="inputs-style">
            <input type="text" id="name" name="message_name" required value="<?php echo esc_attr($_POST['message_name']); ?>">
            <span class="floating-label">YOUR NAME</span>
        </div>

        <div class="inputs-style">
            <input type="text" id="email" name="message_email" required value="<?php echo esc_attr($_POST['message_email']); ?>">
            <span class="floating-label">YOUR E-MAIL</span>
        </div>

        <div class="inputs-style">
            <textarea class="autoExpand" type="text" data-min-rows='' rows="1" required name="message_text"><?php echo esc_textarea($_POST['message_text']); ?></textarea>
            <span class="floating-label">YOUR MESSAGE</span>
        </div>

      <!-- <p>
        <label for="name">Name: <span>*</span> <br>
          <input type="text" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>">
        </label>
      </p>

      <p>
        <label for="message_email">Email: <span>*</span> <br>
          <input type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>">
        </label>
      </p>

      <p>
        <label for="message_text">Message: <span>*</span> <br>
          <textarea type="text" name="message_text">
        <?php echo esc_textarea($_POST['message_text']); ?>
          </textarea>
        </label>
      </p> -->

        <p>
            <label for="message_human">Human Verification: <span>*</span> <br>
                <input type="text" style="width: 60px;" name="message_human"> + 3 = 5
            </label>
        </p>
        <input type="hidden" name="submitted" value="1">

        <input class="btn btn-imperium btn-2" type="submit">
    </form>
</div>

现在,当我直接将其用作网页时效果很好,但是当我尝试导入front-page.php这样的<?php include('page-contact-us.php'); ?>时,它无效。

有谁知道如何让它在我的front-page.php

中运作

2 个答案:

答案 0 :(得分:0)

您确定提供了模板的正确地址吗?它应该是这样的,

<?php include(get_template_directory_uri().'/page-contact-us.php'); ?>

为了确保包含你可能想要将include()函数更改为require()。

答案 1 :(得分:0)

您是否尝试过get_template_part()函数?它是将代码部分导入主题文件的方式。 您可以阅读更多相关信息here