自定义验证消息,用于联系表单7 WordPress

时间:2019-04-09 07:05:53

标签: wordpress contact-form-7

我想显示选择字段的自定义消息。我正在为自定义消息使用插件“联系表单7自定义验证”,但不适用于选择字段。是否有一个钩子可以仅在选择字段中修改我的消息,因为其余的验证消息都可以。

  

更新:

我有以下字段:

<div class="form-half">
                              <label for="state" class="visuallyhidden">state</label>[select* state id:state first_as_label "State" "Alabama" "Alaska" "American Samoa" "Arizona" "Arkansas" "California" "Colorado" "Connecticut" "Delaware" "District of Columbia" "Florida" "Georgia" "Guam" "Hawaii" "Idaho" "Illinois" "Indiana" "Iowa" "Kansas" "Kentucky" "Louisiana" "Maine" "Maryland" "Massachusetts" "Michigan" "Minnesota" "Mississippi" "Missouri" "Montana" "Nebraska" "Nevada" "New Hampshire" "New Jersey" "New Mexico" "New York" "North Carolina" "North Dakota" "Northern Marianas Islands" "Ohio" "Oklahoma" "Oregon" "Pennsylvania" "Puerto Rico" "Rhode Island" "South Carolina" "South Dakota" "Tennessee" "Texas" "Utah" "Vermont" "Virginia" "Virgin Islands" "Washington" "West Virginia" "Wisconsin" "Wyoming"]</div>

我已经使用以下钩子来做到这一点,但不起作用:

add_filter( 'wpcf7_validate_select*', 'custom_select_validation_filter', 20, 2 );

function custom_select_validation_filter( $result, $tag ) {
    if ( 'state' == $tag->name ) {

        echo $test_custom_select = $_POST['state'];
        if ( empty( $test_custom_select ) || $test_custom_select == 'State' ) {
            // Example of result
            $result->invalidate($tag, __( 'Please enter state name', 'CF7' ));
        }

    }

    return $result;
}

但这不起作用。

2 个答案:

答案 0 :(得分:1)

尝试以下代码:

add_filter( 'wpcf7_validate_select', 'custom_select_validation_filter', 20, 2 );


function custom_select_validation_filter( $result, $tag ) {
    if ( 'state' == $tag->name ) {

        $test_custom_select = $_POST['state'];
        if ( empty( $test_custom_select ) || $test_custom_select == 'State' ) {
            // Example of result
            $result->invalidate($tag, __( 'your-select is required', 'CF7' ));
        }

    }
    elseif ( 'second-select' == $tag->name ){

        $test_custom_select = $_POST['second-select'];
        if ( empty( $test_custom_select ) ) {
            // Example of result
            $result->invalidate($tag, __( 'second-select is required', 'CF7' ));
        }

    }

    return $result;
}

https://contactform7.com/2015/03/28/custom-validation/

字段CF7: [选择州ID:州first_as_label“州”“阿拉巴马州”“阿拉斯加”“萨摩亚”“亚利桑那州”“阿肯色州”“加利福尼亚州”“科罗拉多州”“康涅狄格州”“特拉华州”“哥伦比亚特区”“佛罗里达州”“乔治亚州”“关岛”“夏威夷”“爱达荷州”“伊利诺伊州”“印第安纳州”“爱荷华州”“堪萨斯州”“肯塔基州”“路易斯安那州”“缅因州”“马里兰州”“马萨诸塞州”“密歇根州”“明尼苏达州”“密西西比州”“密苏里州”“蒙大拿州” “内布拉斯加州”,“内华达州”,“新罕布什尔州”,“新泽西州”,“新墨西哥州”,“纽约州”,“北卡罗来纳州”,“北达科他州”,“北马里亚纳群岛”,“俄亥俄州”,“俄克拉荷马州”,“俄勒冈州”,“宾夕法尼亚州”,“波多黎各“”罗德岛“”南卡罗来纳州“”南达科他州“”田纳西州“”得克萨斯州“”犹他州“”佛蒙特州“”弗吉尼亚州“”维尔京群岛“”华盛顿“”西弗吉尼亚州“”威斯康星州“”怀俄明州“]

答案 1 :(得分:0)

经测试可正常工作。

// For the custom Price for shuttle transport
/**
 * Generates a HTML string of two or more `option` elements/tags.
 *
 * @see wpcf7_select_shuttleprice_form_tag_handler()
 *
 * @return string $html
 */
function shuttleprice() {

    $id_a = null;      
    $max_personen = get_field("maximale_anzahl", $id_a);
    $max_personen_gesamt = get_field("anzahl_maximale_personen_im_shuttle_mit_aufpreis", $id_a);
    $aufpreis = get_field("aufpreis_pro_person_im_shuttle", $id_a);

    $inkl = "";
    $more = "";

    for ($x = 1; $x <= $max_personen; $x++) {
        if($x == 1) {
            $inkl = $inkl."<option value='".$x."'>für ".$x." Person (inklusive)</option>";
        } else {
            $inkl = $inkl."<option value='".$x."'>für ".$x." Personen (inklusive)</option>";
        }
    }

    if($max_personen_gesamt != "") {
        $lauf = 1;
        for ($x = $max_personen + 1; $x <= $max_personen_gesamt; $x++) {
            $more = $more.'<option data-price="'.$aufpreis*$lauf.'" value="'.$x.'">für '.$x.' Personen ('.$aufpreis*$lauf.' € Aufpreis)</option>';
            $lauf++;
        }
    }

    $html = '<option value="0">bitte wählen</option>'.$inkl.$more;

    return $html;
}


add_action( 'wpcf7_init', 'wpcf7_add_form_tag_select_shuttleprice' );
function wpcf7_add_form_tag_select_shuttleprice() {
    wpcf7_add_form_tag(
        array(
            'select_shuttleprice',
            'select_shuttleprice*',
        ),
        'wpcf7_select_shuttleprice_form_tag_handler',
        array(
            'name-attr'         => true,
            'selectable-values' => true,
        )
    );
}

function wpcf7_select_shuttleprice_form_tag_handler( $tag ) {
    return str_replace( '</select>', shuttleprice() . '</select>', str_replace(
        '<option value="">---</option>', '', wpcf7_select_form_tag_handler( $tag )
    ) );
}


add_filter( 'wpcf7_validate_select_shuttleprice', 'wpcf7_select_shuttleprice_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_select_shuttleprice*', 'wpcf7_select_shuttleprice_validation_filter', 10, 2 );
function wpcf7_select_shuttleprice_validation_filter( $result, $tag ) {
    $name = $tag->name;
    $empty = ( empty( $_POST[ $name ] ) || '0' === $_POST[ $name ] );

    if ( $tag->is_required() && $empty ) {
        $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
    }

    return $result;
}

使用此简码

[select_shuttleprice* shuttleprice-1 class:shuttleprice]