如何在联系表格7中制作自定义表格标签 [英] How to make custom form-tag in contact form 7 required

查看:81
本文介绍了如何在联系表格7中制作自定义表格标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在联系表格7中创建自定义表格标签!这是我的课程列表的下拉列表,现在我想要求它是因为这是整体形式的主要内容。
那么有人可以给我小费怎么做吗?



当我执行 [myCustomField *课程名称类:custom-field]



它不能与 *
一起使用,因此,如果有人可以帮助,那就太好了!

解决方案

我今天下午一直在为此工作,我认为Mahmoud并未添加进行验证所需的一切



使用我从联系表7上的帖子中学到的信息:
https://contactform7.com/2015/01/10/adding-a-custom-form-tag
https:/ /contactform7.com/2015/02/27/using-values-from-a-form-tag/



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



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

  add_action('wpcf7_init','custom_add_form_tag_myCustomField'); 

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

函数custom_myCustomField_form_tag_handler($ tag){

$ tag =新的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吗? '真假';

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

$ atts = wpcf7_format_atts($ atts);

$ myCustomField =’;

$ query =新的WP_Query(array(
'post_type'=>'自定义帖子类型',
'post_status'=>'发布',
'posts_per_page'=> -1,
'orderby'=>'title',
'order'=>'ASC',
)));

而($ 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
);

返回$ myCustomField;
}

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



然后,要完成此操作,我们需要添加一些验证通过过滤器。



目前尚无相关文档,但是我使用了select.php中的功能并将其更改为所需的功能。

  / *验证过滤器* / 

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

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

$ name = $ tag->名称;

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

$ empty =! isset($ _POST [$ name])||空($ _POST [$ name])&& ‘0’!== $ _POST [$ name];

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

返回$结果;
}

此代码也应该放在您的functions.php文件中,



此处,过滤器的第一个字符串$ tag应该与自定义CF7标签中生成的类匹配,因此,如果您的自定义tag-> type = 'myCustomField',则过滤器的$ tag必须包含名称,例如wpcf7_validate_myCustomField以及所需的版本wpcf7_validate_myCustomField *。



如果您想从Contact Form 7的后端获得更多选项,请检查select.php文件,因为它在布局上相当不错如何获得每个选项并将其包括在内。


So i make custom form-tag in contact form 7! It is a drop down with list of my courses and now I want to make it required because that is the main thing in whole form. So can someone give me a tip how to do that?

When I do the [myCustomField* course-name class:custom-field]

It does not working with * So if someone can help it will be great!

解决方案

I have been working on this myself this afternoon and I do not think Mahmoud has added everything that is needed to get the validation working well and the messages showing up.

using what I have learnt from the posts on contact form 7 here: https://contactform7.com/2015/01/10/adding-a-custom-form-tag https://contactform7.com/2015/02/27/using-values-from-a-form-tag/

and looking at this file in the plugin: contact-form-7/modules/select.php which helped a lot.

I think this will work better and needs to be added to your functions.php file in your child-theme.

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

That is how we create the custom tag. The important differences here are the addition of the $validation_error variables as wells the aria-required and aria-invalid data. It is also important to include the $validation_error in the final output so that we can see the validation messages being created.

Then to finish it off we need to add some validation via filters.

There is no documentation on this yet, but I used the functions from the select.php and altered them to what I needed.

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

This code should also go in your functions.php file just under the code for the custom CF7 tag.

Here the filter's first string $tag should match with the class that is being generated in the custom CF7 tag so if your custom tag->type = 'myCustomField' then the $tag of the filter must include the name, like so wpcf7_validate_myCustomField as well as the required version of it, wpcf7_validate_myCustomField*.

I hope that helps anyone else looking for this.

If you want even more of the options available from the backend of Contact Form 7 check the select.php file as it lays it out quite nicely on how to get each option and include it.

这篇关于如何在联系表格7中制作自定义表格标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆