如何在需要的联系表单 7 中制作自定义表单标签

Mil*_*nan 4 wordpress contact-form-7

所以我在联系表 7 中制作了自定义表单标签!这是我的课程列表的下拉列表,现在我想将其设为必需,因为这是整个形式的主要内容。那么有人可以给我提示如何做到这一点吗?

当我做的时候[myCustomField* course-name class:custom-field]

它不适用于* 因此,如果有人可以提供帮助,那就太好了!

Cly*_*mas 5

今天下午我自己一直在研究这个问题,我认为 Mahmoud 没有添加使验证正常运行和显示消息所需的所有内容。

使用我从联系表格 7 上的帖子中学到的知识: https://contactform7.com/2015/01/10/adding-a-custom-form-tag https://contactform7.com/2015/02/27 /使用表单标签中的值/

并在插件中查看此文件:contact-form-7/modules/select.php 这很有帮助。

我认为这会更好,需要添加到您的子主题中的functions.php 文件中。

add_action( 'wpcf7_init', 'custom_add_form_tag_myCustomField' );

function custom_add_form_tag_myCustomField() {
    wpcf7_add_form_tag( array( 'myCustomField', 'myCustomField*' ), 
'custom_myCustomField_form_tag_handler', true );
}

function custom_myCustomField_form_tag_handler( $tag ) {

    $tag = new WPCF7_FormTag( $tag );

    if ( empty( $tag->name ) ) {
        return '';
    }

    $validation_error = wpcf7_get_validation_error( $tag->name );

    $class = wpcf7_form_controls_class( $tag->type );

    if ( $validation_error ) {
        $class .= ' wpcf7-not-valid';
    }

    $atts = array();

    $atts['class'] = $tag->get_class_option( $class );
    $atts['id'] = $tag->get_id_option();

    if ( $tag->is_required() ) {
    $atts['aria-required'] = 'true';
    }

    $atts['aria-invalid'] = $validation_error ? 'true' : 'false';

    $atts['name'] = $tag->name;

    $atts = wpcf7_format_atts( $atts );

    $myCustomField = '';

    $query = new WP_Query(array(
        'post_type' => 'CUSTOM POST TYPE HERE',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby'       => 'title',
        'order'         => 'ASC',
    ));

    while ($query->have_posts()) {
        $query->the_post();
        $post_title = get_the_title();
        $myCustomField .= sprintf( '<option value="%1$s">%1$s</option>', 
esc_html( $post_title ) );
    }

    wp_reset_query();

    $myCustomField = sprintf(
        '<span class="wpcf7-form-control-wrap %1$s"><select %2$s>%3$s</select>%4$s</span>',
        sanitize_html_class( $tag->name ),
        $atts,
        $myCustomField,
        $validation_error
    );

    return $myCustomField;
}
Run Code Online (Sandbox Code Playgroud)

这就是我们创建自定义标签的方式。这里的重要区别是添加了 $validation_error 变量以及 aria-required 和 aria-invalid 数据。在最终输出中包含 $validation_error 也很重要,以便我们可以看到正在创建的验证消息。

然后为了完成它,我们需要通过过滤器添加一些验证。

目前还没有这方面的文档,但我使用了 select.php 中的函数并将它们更改为我需要的。

/* Validation filter */

add_filter( 'wpcf7_validate_myCustomField', 'wpcf7_myCustomField_validation_filter', 10, 2 );
add_filter( 'wpcf7_validate_myCustomField*', 'wpcf7_myCustomField_validation_filter', 10, 2 );

function wpcf7_myCustomField_validation_filter( $result, $tag ) {
    $tag = new WPCF7_FormTag( $tag );

    $name = $tag->name;

    if ( isset( $_POST[$name] ) && is_array( $_POST[$name] ) ) {
        foreach ( $_POST[$name] as $key => $value ) {
            if ( '' === $value ) {
                unset( $_POST[$name][$key] );
            }
        }
    }

    $empty = ! isset( $_POST[$name] ) || empty( $_POST[$name] ) && '0' !== $_POST[$name];

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

    return $result;
}
Run Code Online (Sandbox Code Playgroud)

此代码还应位于您的functions.php 文件中,位于自定义CF7 标记的代码下方。

这里过滤器的第一个字符串 $tag 应与自定义 CF7 标记中生成的类匹配,因此如果您的自定义 tag->type = 'myCustomField' 则过滤器的 $tag 必须包含名称,就像 wpcf7_validate_myCustomField 一样作为其所需版本,wpcf7_validate_myCustomField*。

我希望这能帮助其他正在寻找这个的人。

如果您想要从 Contact Form 7 后端获得更多可用选项,请检查 select.php 文件,因为它很好地说明了如何获取每个选项并包含它。