使用Contact Form 7创建自定义标记/字段



我想在Contact表单7中创建一个自定义的textrea字段。如果我可以用数据预先填充它。我在前端显示了下面的内容,并创建了一个文本区域字段。但当我提交表单时,电子邮件中的标签总是空白的,所以数据不会通过电子邮件发送。该标签也不会显示在联系人表单7中的可用标签列表中。有什么想法吗?

add_action( 'wpcf7_init', 'wpcf7_add_form_tag_pathtag' );
function wpcf7_add_form_tag_pathtag() {
wpcf7_add_form_tag(
array( 'pathtag', 'pathtag*'), 'pathtag_form_tag_handler',  array( 'name-attr' => true )
);
}
function pathtag_form_tag_handler( $tag ) {
$tag = new WPCF7_FormTag( $tag );
if ( empty( $tag->name ) ) {
return '';
}
$atts = array();
$class = wpcf7_form_controls_class( $tag->type );
$atts['class'] = $tag->get_class_option( $class );
$atts['id'] = $tag->get_id_option();
$atts['name'] = $tag->name;
$atts = wpcf7_format_atts( $atts );
$output = '';
$output .= '<textarea cols="40" rows="10">';
foreach ( $_SESSION['question-path'] as $path ) {
$output .= $path ."nn";
}
$output .= '</textarea>';
return $output;
}

您没有将属性添加到文本区域代码中。此外,请确保您在联系表格中输入您的短代码作为

[pathtag field-name]

function pathtag_form_tag_handler($tag) {
$tag = new WPCF7_FormTag($tag);
if (empty($tag->name)) {
return '';
}
$atts = array();
$class = wpcf7_form_controls_class($tag->type);
$atts['class'] = $tag->get_class_option($class);
$atts['id'] = $tag->get_id_option();
$atts['name'] = $tag->name;
$atts = wpcf7_format_atts($atts);
$output = '';
// Make sure to add the $class and $atts to the form tag
$output .= sprintf('<textarea cols="40" rows="10" %s %s>', $class, $atts);
foreach ($_SESSION['question-path'] as $path) {
$output .= $path . "nn";
}
$output .= '</textarea>';
return $output;
}

最新更新