簡體   English   中英

聯系Form 7自定義驗證以獲取特定的表單ID

[英]Contact Form 7 custom validation for specific form id

我在聯系表單7中使用自定義驗證,但是我需要針對特定表單進行驗證,而不必針對我的所有表單進行驗證。 這是我的代碼:

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;
}

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM