2016-07-14 82 views
0

我使用自定義的驗證在聯繫表7,但我需要驗證只針對特定的形式,而不是我的所有形式。這是我的代碼:聯繫表7的自定義驗證的具體形式ID

add_filter('wpcf7_validate_text*', 'my_custom_text_validation_filter', 20, 2); 
function my_custom_text_validation_filter($result, $tag) { 

    $tag = new WPCF7_Shortcode($tag); 

    if ('name' == $tag->name) { // validate name field only 

     .... // my validation here 

    } 

    return $result; 
} 

回答

1

CF7總是添加到形式的隱藏字段名爲_wpcf7,包含表單ID。在執行代碼之前,可以使用該字段來檢查您正在驗證的表單:

add_filter('wpcf7_validate_text*', 'my_custom_text_validation_filter', 20, 2); 
function my_custom_text_validation_filter($result, $tag) { 

    if (isset($_POST['_wpcf7']) && $_POST['_wpcf7'] != 166) // Only form id 166 will be validated. 
     return $result; 

    $tag = new WPCF7_Shortcode($tag); 

    if ('name' == $tag->name) { // validate name field only 

     .... // my validation here 

    } 

    return $result; 
}