【发布时间】:2017-08-05 03:31:08
【问题描述】:
所以我在联系表单 7 中制作了自定义表单标签!这是一个包含我的课程列表的下拉列表,现在我想让它成为必需的,因为这是整个形式的主要内容。 那么有人可以给我一个提示如何做到这一点吗?
当我做[myCustomField* course-name class:custom-field]时
它不适用于 * 因此,如果有人可以提供帮助,那就太好了!
【问题讨论】:
所以我在联系表单 7 中制作了自定义表单标签!这是一个包含我的课程列表的下拉列表,现在我想让它成为必需的,因为这是整个形式的主要内容。 那么有人可以给我一个提示如何做到这一点吗?
当我做[myCustomField* course-name class: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' );
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;
}
这就是我们创建自定义标签的方式。这里的重要区别是添加了 $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;
}
这段代码也应该放在你的functions.php文件中,就在自定义CF7标签的代码下面。
这里过滤器的第一个字符串 $tag 应该与自定义 CF7 标记中生成的类匹配,因此如果您的自定义 tag->type = 'myCustomField' 则过滤器的 $tag 必须包含名称,如下所示wpcf7_validate_myCustomField 及其所需版本 wpcf7_validate_myCustomField*。
我希望这可以帮助其他正在寻找这个的人。
如果您想从 Contact Form 7 的后端获得更多可用选项,请检查 select.php 文件,因为它很好地说明了如何获取每个选项并包含它。
【讨论】:
您可以使用[select*] 输出所需的下拉菜单。
[select* course-name include_blank "English" "Math"]
查看https://contactform7.com/checkboxes-radio-buttons-and-menus/
编辑:
所以你有自己的短代码[myCustomField]。要将您的简码的两个版本设为 [myCustomField] 和 [myCustomField*],您必须将两个简码传递给您的函数,如下所示:
add_action( 'wpcf7_init', 'wpcf7_add_form_tag_mycustomfield' );
function wpcf7_add_form_tag_mycustomfield() {
wpcf7_add_form_tag( array( 'myCustomField', 'myCustomField*'),
'wpcf7_mycustomfield_form_tag_handler', array( 'name-attr' => true ) );
}
function wpcf7_mycustomfield_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 );
$html = sprintf( '<your-tag %s></your-tag>', $atts );
return $html;
}
然后,你就可以使用它了:
[myCustomField course-name class:custom-field]
或
[myCustomField* course-name class:custom-field]
参考资料:
【讨论】: