2017-10-16 45 views
0

你好傢伙我正在嘗試使自定義聯繫表單,我有一些問題。 我在我的主題文件中製作了page-contact-us.php,並在CMS中創建了聯繫我們頁面。 這是代碼:在front-page.php中導入聯繫表格

<?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

解釋「它不起作用」,它給你什麼錯誤? – NoImaginationGuy

+0

@NoImaginationGuy對不起,沒有提供更多的細節,但事情是我沒有得到任何錯誤。當我使用聯繫我們頁面時,表單可以工作,但是當在front-page.php中導入時,它不會,但它會將我重定向到我的自定義信息類型中的某個帖子。說實話沒有意義。 – Zvezdas1989

+0

代碼格式化,頁面名稱高亮 –

回答

0

您確定提供了模板的正確地址嗎?它應該是這樣的,

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

爲了確保它包括你可能想要將include()函數改爲require()。

+0

地址是好的。 HTML部分可以在頁面上清楚地看到。當我試圖包含上面的代碼時,我得到這個'Warning:include():http:// wrapper在服務器配置中被allow_url_include = 0在C:\ wamp \ www \ imperum \ wp-content \ themes \在331行上的'imperum \ front-page.php'和'Warning:include(http://localhost/imperum/wp-content/themes/imperum/page-contact-us.php):未能打開流:不適合可以在第331行的C:\ wamp \ www \ imperum \ wp-content \ themes \ imperum \ front-page.php中找到封裝器。 – Zvezdas1989

+0

您需要轉到WAMP中的php.ini文件並將'allow_url_include = 0 'to'allow_url_include = 1' –

+0

我可以試試,但我已經直接在服務器上試過了你的代碼,沒有運氣。當我在服務器上使用你的代碼時,什麼都不會發生沒有東西被導入。 – Zvezdas1989

0

您是否嘗試過get_template_part()函數?這是代碼部分旨在導入到主題文件中的方式。 你可以閱讀更多關於它here

+0

同樣的事情發生在常規進口。它導入代碼,但它不起作用。它將我重定向到我的自定義帖子類型中的一篇帖子。說實話沒有意義。 – Zvezdas1989

+0

您是否在創建自定義帖子類型模板後刷新了固定鏈接? – bojan259

+0

是的,我已經做到了。 – Zvezdas1989