gpt4 book ai didi

wordpress - 如何在联系表格 7 中制作自定义表格标签

转载 作者:行者123 更新时间:2023-12-04 00:35:56 25 4
gpt4 key购买 nike

所以我在联系表格 7 中制作了自定义表格标签!这是我的类(class)列表的下拉菜单,现在我想将其设为必需,因为这是整个形式的主要内容。
那么有人可以给我一个提示如何做到这一点吗?

当我做 [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 标签中生成的类匹配,所以如果您的自定义标签->type = 'myCustomField' 那么过滤器的 $tag 必须包含名称,就像 wpcf7_validate_myCustomField 一样作为它的必需版本,wpcf7_validate_myCustomField*。

我希望能帮助其他人寻找这个。

如果您想要更多来自 Contact Form 7 后端的可用选项,请检查 select.php 文件,因为它非常好地介绍了如何获取每个选项并将其包含在内。

关于wordpress - 如何在联系表格 7 中制作自定义表格标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42792051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com