因此,我正在嘗試生成一個動態隱藏文本欄位,該欄位將在 Contact Form 7 中包含隨機的字母和數字字串。
我試過下面的代碼
add_action('wpcf7_init', 'custom_code_generator');
function custom_code_generator(){
wpcf7_add_form_tag('coupon_code', 'custom_code_handler');
}
function custom_code_handler($tag){
$input_code_name = 'rand_string';
$charsList = '1234567890abcdefghijklmnopqrstuvwxyz';
$randomString = '';
for ($i=4; $i < 10; $i ){
$randomString .= $charsList[rand(0, strlen($charsList))];
}
$finalCode = strtoupper($randomString);
$html = '<input type="hidden" name="'.$input_code_name.' "value=" '.$finalCode . ' " />';
return $html;
}
基于此方法 https://www.wpguru.com.au/generate-dynamic-tag-contact-form-7/
但是它似乎對我不起作用,我將代碼添加到函式中并嘗試對其進行大量編輯,但沒有明確的結果。
主要思想是我需要為每個 CF7 提交一個唯一的字串,所有提交都存盤到 Wordpress 中,并且資料的副本也被發送到另一個資料庫。
將短代碼 [coupon_code] 添加到郵件模板不會回傳任何內容,并且似乎根本沒有資料通過該欄位存盤。
uj5u.com熱心網友回復:
你所擁有的應該基本上是有效的。我通過洗掉一些無關的宣告進行了一些調整并優化了您的一些代碼,并且值周圍還有額外的空格。
add_action( 'wpcf7_init', 'custom_code_generator' );
function custom_code_generator() {
wpcf7_add_form_tag( 'coupon_code', 'custom_code_handler' );
}
function custom_code_handler() {
$characters = '1234567890abcdefghijklmnopqrstuvwxyz';
$random_string = '';
for ( $i = 4; $i < 10; $i ) {
$random_string .= $characters[ wp_rand( 0, strlen( $characters ) ) ];
}
return '<input type="hidden" name="rand_string" value="' . strtoupper( $random_string ) . '" />';
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/421556.html
標籤:
下一篇:SVG頂部的額外空間
