代码之家  ›  专栏  ›  技术社区  ›  Cray

通过短代码WordPress向联系人表单7添加自定义文本

  •  0
  • Cray  · 技术社区  · 6 年前

    我正在使用联系人fom 7,需要在表单的某个位置添加自定义文本。

    我将从高级自定义字段的子字段中获取使用PHP的自定义文本。我知道,有一个额外的插件叫做“联系人表单7动态文本扩展”。( https://de.wordpress.org/plugins/contact-form-7-dynamic-text-extension/ )但是因为文本在子字段中,所以我不能使用它。

    所以,我需要的是隐藏输入字段或表单生成的电子邮件中的文本。

    我认为CF7短代码本身就是一个自定义参数。这样地:

    [contact-form-7 id="1" title="Title" customtext="Text"]
    

    有可能吗?

    或者,是否可以使用标题并将其添加到输入字段或表单的电子邮件中?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Vasim Shaikh    6 年前

    您有一个名为__customtext_的字段用于目标电子邮件地址:

    [text* customtext]
    

    要从shortcode属性中获取默认值,请将default:shortcode_attr选项添加到表单标记:

    [text* customtext default:shortcode_attr]
    

    然后,将与字段同名的属性(在本例中为__customtext_)添加到联系人表单的短代码中:

    [contact-form-7 id="123" title="Contact Form" customtext="xxxxxx@example.com"]
    

    您需要预先注册属性。

    将以下代码段添加到主题_S functions.php文件中:

    add_filter( 'shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3 );
    
    function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) {
        $my_attr = 'customtext;
    
    if ( isset( $atts[$my_attr] ) ) {
        $out[$my_attr] = $atts[$my_attr];
    }
    
    return $out;
    }
    

    引用链接: https://contactform7.com/getting-default-values-from-shortcode-attributes/

        2
  •  1
  •   Alice    6 年前

    首先在PHP中添加字段

    function cf7_add_my_field(){
    
        global $post;
        return $post->ID; // Here instead of returning post id, get your meta/submeta field and return that value.
    }
    
    add_shortcode('CF7_ADD_MY_FIELD', 'cf7_add_my_field');
    

    在这之后,在表单中添加以下内容-

    [dynamictext my-filed-name "CF7_ADD_MY_FIELD"]
    

    注意:您需要“联系人表单7动态文本扩展”插件,这是您已经提到的。