Wordpress 如何创建联系人表单7自定义字段?

Wordpress 如何创建联系人表单7自定义字段?,wordpress,contact-form-7,Wordpress,Contact Form 7,如何在wordpress联系人表单7中创建自定义表单标签 然后将结果添加到发送的电子邮件中?好的,我自己解决了这个问题,并想分享我的代码 在本例中,我们将通过添加自定义标记来创建一个自定义标记,该标记显示6个复选框,其中包含最新帖子: [posts show:12] 我们必须钩住3个函数 一个用于注册“posts”标签 另一个用于“清理”答案(因为它们是一个数组) 另一个用于注册“邮件”部分中使用的“posts”标记 要点如下: 代码如下: 非常有用,陛下。感谢分享您的决议:) <

如何在wordpress联系人表单7中创建自定义表单标签
然后将结果添加到发送的电子邮件中?

好的,我自己解决了这个问题,并想分享我的代码

在本例中,我们将通过添加自定义标记来创建一个自定义标记,该标记显示6个复选框,其中包含最新帖子:

[posts show:12]
我们必须钩住3个函数

  • 一个用于注册“posts”标签
  • 另一个用于“清理”答案(因为它们是一个数组)
  • 另一个用于注册“邮件”部分中使用的“posts”标记
要点如下:

代码如下:
非常有用,陛下。感谢分享您的决议:)
<?php
/**
 * Contact form 7
 * custom tag: [posts show:12]
 * show parameter is optional
 */
add_action('wpcf7_init', 'custom_add_form_tag_posts');

function custom_add_form_tag_posts()
{
    wpcf7_add_form_tag('posts', 'custom_posts_form_tag_handler');
}

function custom_posts_form_tag_handler($tag)
{
    //get current (local) date
    $blogtime = current_time('mysql');
    list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = preg_split('([^0-9])', $blogtime);

    //how may to show? (default 6)
    $numberPosts=6;
    $show=$tag->get_option('show', 'int', true);

    $args = [
        'post_type'     => 'posts',
        'posts_per_page'=> $show ? $show : $numberPosts,        
        'order'         => 'ASC'
    ];

    // The Query
    $the_query = new WP_Query($args);

    // The Loop
    $rows=[];
    if ($the_query->have_posts()) {
        while ($the_query->have_posts()) {
            $the_query->the_post();
            $rows[]=[
                'id'        =>get_the_ID(),
                'title'     =>get_the_title(),
             ];
        }
        wp_reset_postdata();
    }
    // debug your query
    // echo $the_query->request;

    // Structure
    $res="<div class='12u'><h3 class='mb-0'>No posts to display</h3></div>";

    if ($rows) {
        $res="<div class='row'>";

        foreach ($rows as $row) {
            $res.="<div>";
            $res.='<input type="checkbox" name="posts[]" value="'.esc_html($row['title']).'" id="'.esc_html($row['id']).'" />';
            $res.='<label for="'.esc_html($row['id']).'">'.esc_html($row['title']).' <br>';
            $res.= esc_html($row['title'])
            $res.=' </label>';
            $res.="</div>";
        }
        $res.="</div>";
    }

    return $res;
}

/**
 * When saving, change the array to a comma, separated list, just to make it easier 
 */
add_filter("wpcf7_posted_data", function ($posted_data) {
    //'posts' is the name that you gave the field in the CF7 admin.
    if (isset($posted_data['posts'])) {
        $posted_data['posts'] = implode(", ", $posted_data['posts']);
    }

    return $posted_data;
});

/**
 * A tag to be used in "Mail" section so the user receives the special tag
 * [posts]
 */
add_filter('wpcf7_special_mail_tags', 'wpcf7_tag_post', 10, 3);
function wpcf7_tag_post($output, $name, $html)
{
    $name = preg_replace('/^wpcf7\./', '_', $name); // for back-compat

    $submission = WPCF7_Submission::get_instance();

    if (! $submission) {
        return $output;
    }

    if ('posts' == $name) {
        return $submission->get_posted_data("posts");
    }

    return $output;
}