如何添加自定义属性? [英] How to add a custom attribute?

查看:177
本文介绍了如何添加自定义属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在字段联系表7 没有javascript 的情况下添加自定义属性?

How to add a custom attribute in the field Contact Form 7 without javascript ?

例如,页面上有这样一个字段:

For example, there is such a field on the page:

<input type="text" name="name" class="form-control" id="name-1" data-attr="custom" data-msg="Текст 1"> 

问题:是否可以设置这些自定义属性( data-attr data-msg )在管理面板中的字段?

Question: is it possible to set these custom attributes (data-attr, data-msg) of fields in the admin panel?

推荐答案

找到您的字段名称。

[text* text-21]

如果您的字段名称为 text-21(例如在我的示例中),请添加

If the name of your field name="text-21", like in my example, add this code to functions.php file.

add_filter( 'wpcf7_form_elements', 'imp_wpcf7_form_elements' );
function imp_wpcf7_form_elements( $content ) {
    $str_pos = strpos( $content, 'name="text-21"' );
    if ( $str_pos !== false ) {
        $content = substr_replace( $content, ' data-attr="custom" data-msg="Foo Bar 1" ', $str_pos, 0 );
    }
    return $content;
}

注意,它将这些属性添加到名称为text的所有表单元素中-21,如果要阻止它,则给表单元素一些唯一的名称[text * unique-name]

Note, it will add those attributes to all forms elements where the name is text-21, if you want prevent it then give your form element some unique name [text* unique-name]

并将代码更改为

add_filter( 'wpcf7_form_elements', 'imp_wpcf7_form_elements' );
function imp_wpcf7_form_elements( $content ) {
    $str_pos = strpos( $content, 'name="unique-name"' );
    if ( $str_pos !== false ) {
        $content = substr_replace( $content, ' data-attr="custom" data-msg="Foo Bar 1" ', $str_pos, 0 );
    }
    return $content;
}

这篇关于如何添加自定义属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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