2016-05-27 156 views
2

我不能得到這個工作。似乎不可能獲得任何真實的文件記錄。Wordpress聯繫表單7自定義短代碼的收件人

我在我的functions.php中有一個自定義短代碼,它從外部API獲取電子郵件地址。我想在WPCF7中的「TO」(即收件人)字段中使用此簡碼。但它一直給我一個「語法錯誤」的錯誤警告。

我需要找到一種方法,使收件人字段從我的functions.php文件接受自定義短代碼。我試過the dynamic text plugin但它不起作用。

這裏是我的簡碼的例子

function single_email() { 
    global $contact; 
    return $contact->email; 
}add_shortcode('contact_email', 'single_email'); 

也許一起使用 「wpcf7_special_mail_tags」 和 「do_shortcode()」 的行的東西嗎?不確定那是如何工作的。

使用WPCF7的版本4.4.2。

+0

你的意思是,你必須創建'CONTACT_EMAIL創建Ajax網頁「簡碼對吧? – purvik7373

+0

我的不好!是的,add_shortcode。我已經糾正了這個問題。 – axelra82

回答

1

請問您能檢查下面的代碼嗎?

wpcf7_add_shortcode('contact_email', 'single_email', true); 
function single_email(){ 
    global $contact; 
    return $contact->email; 
} 
+0

對不起,不行。相同的語法錯誤警告。 – axelra82

+0

你能解釋我什麼是錯誤? – purvik7373

+0

錯誤僅僅是「to」字段的「語法錯誤」。爲了更好的衡量,版本4.4.2是我目前的WPCF版本。 – axelra82

1

您可以像這樣使用在wp_options表中創建新字段並使用像這樣。

在您的聯繫人頁面

 <div class="contant"> 
     <div class="section"> 
     <div class="contact_pannel"> 
      <h1>You can find us literally anywhere, just push a button and we’re there</h1> 
      <div class="lf_pannel"> 
      <form action="" method="" id='myForm' onsubmit=" return pretendValidation(); "> 
      <h2>Use this form to send us a message:</h2> 
      <div class="fild"> 
       <label class="label">Name</label> 
       <input name="Name" type="text" class="input"> 
      </div> 

      <div class="fild"> 
       <label class="label">Email</label> 
       <input name="Email" type="email" class="input" required=required > 
      </div> 

      <div class="fild"> 
       <label class="label">Company</label> 
       <input name="Company" type="text" class="input"> 
      </div> 

      <div class="fild"> 
       <label class="label">Subject</label> 
       <input name="Subject" type="text" class="input"> 
      </div> 

      <div class="fild"> 
       <label class="label">Message</label> 
       <textarea class="input" style="height:90px;" id='Message'></textarea> 
      </div> 
       <input name="submitt" type="submit" id="submitt" style="display:none;"> 

      <a href="javascript:void(0);" class="send_button margin_right23" onclick="document.getElementById('mailed').style.display = 'none'; document.getElementById('submitt').click();" >Send Message</a> 
      </form> 

      <div class=" wpcf7-display-none " id="mailed" role="alert">Your message was sent successfully. Thanks.</div> 
      </div> 

      <div class="rg_pannel"> 


      <?php echo html_entity_decode( get_option('admin_address'));?> 





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

    <script> 
    function pretendValidation(){ 


    jQuery(document).ready(function($) { 

     // We'll pass this variable to the PHP function example_ajax_request 
     var fruit = 'Banana'; 
      // console.log($("#myForm").serialize()); 
     // This does the ajax request 

     $.post("<?=get_template_directory_uri() ;?>/contact_ajax.php", 
     { myForm: $("#myForm").serialize(), Message: $("#Message").val() }, 
     function(data) { 
      console.log(data); // John 

     }); 




     $('#myForm')[0].reset(); 

     $("#mailed").show("slow"); 

    }); 
     return false; 
    } 
    </script> 

之後,你必須這樣

contact_ajax.php

<?php 

/** Load WordPress Bootstrap */ 
require_once ('../../../wp-load.php'); 

// echo "1232".site_url(); 


// print_r($_REQUEST); 

$params = array(); 
parse_str($_REQUEST['myForm'], $params); 
$params['Message']= $_REQUEST['Message']; 
// print_r($params); 

$to  = get_option('your_email_field_name'); 
// $to  = "[email protected]"; 
$subject = 'Fokus Tech Contact Form'; 
    $message = "Hello,\n" 
      ."Name: ".$params ['Name']."\n" 
      ."Email: ". $params ['Email']."\n" 
      ."Company: ".$params ['Company']."\n" 
      ."Subject: ". $params ['Subject']."\n" 
      ."Message: ".$params['Message'] ."\n" 

; 
$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

mail($to, $subject, $message, $headers); 

?> 
+0

謝謝。但我需要使用WPCF7來解決這個問題;) – axelra82

相關問題