You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于WooCommerce/FooEvents的WP站点活动建议表单用户自动识别咨询

自动识别登录用户的活动建议表单解决方案

针对你的需求——在已集成WooCommerce、FooEvents和WooCommerce Memberships的WordPress站点中,创建无需用户填写邮箱/用户名的活动建议表单,我来分情况给你详细解答:

核心逻辑:利用WordPress登录用户机制

不管用哪种表单工具,核心都是借助WordPress内置的登录用户检测函数:

  • is_user_logged_in():判断当前访问者是否已登录
  • wp_get_current_user():获取当前登录用户的完整信息(ID、邮箱、会员等级等)

只要用户处于登录状态,系统就能自动获取这些数据,无需用户手动输入。


方案一:用Contact Form7实现(可行,需少量代码)

Contact Form7完全可以满足需求,步骤如下:

1. 创建基础表单

在CF7后台新建表单,只保留核心的建议内容字段,比如:

<label>你的活动建议
    [textarea event_suggestion placeholder="请描述你想建议的活动..." required]
</label>

[submit "提交建议"]

2. 添加自动获取用户信息的代码

在主题的functions.php文件(或自定义插件)中添加以下代码,用于在表单提交时自动抓取当前登录用户的信息,并附加到提交数据中:

add_action( 'wpcf7_before_send_mail', 'cf7_attach_user_data_to_suggestion' );
function cf7_attach_user_data_to_suggestion( $contact_form ) {
    // 仅处理目标表单(替换为你的表单ID或标题)
    if ( $contact_form->id() !== 123 ) return;

    if ( is_user_logged_in() ) {
        $current_user = wp_get_current_user();
        $submission = WPCF7_Submission::get_instance();
        
        if ( $submission ) {
            $posted_data = $submission->get_posted_data();
            // 添加用户核心信息
            $posted_data['user_id'] = $current_user->ID;
            $posted_data['user_name'] = $current_user->display_name;
            $posted_data['user_email'] = $current_user->user_email;
            
            // 若需获取会员信息(WooCommerce Memberships)
            $user_memberships = wc_memberships_get_user_memberships( $current_user->ID );
            $membership_levels = [];
            foreach ( $user_memberships as $membership ) {
                $membership_levels[] = $membership->plan->name;
            }
            $posted_data['user_membership'] = implode( ', ', $membership_levels );
            
            // 更新提交数据,让邮件能调用这些字段
            $submission->set_posted_data( $posted_data );
        }
    }
}

3. 配置邮件模板

在CF7的邮件设置中,你可以用以下标签显示用户信息:

活动建议内容:
[event_suggestion]

提交用户信息:
用户ID:[user_id]
用户名:[user_name]
邮箱:[user_email]
会员等级:[user_membership]

4. 可选:在表单页面显示已识别的用户

为了让用户明确知道系统已识别他们,可以在表单上方添加一段提示:

<?php if ( is_user_logged_in() ) : ?>
    <p>你好,<?php echo wp_get_current_user()->display_name; ?>,请提交你的活动建议:</p>
<?php else : ?>
    <p>请先登录后提交活动建议</p>
<?php endif; ?>

方案二:无需代码的替代方案(免费优先)

如果你不想写代码,这些表单工具的免费版就能轻松实现:

1. Gravity Forms(免费核心版)

  • 新建表单时,添加“隐藏字段”,在字段设置的「动态填充」选项中,选择「当前用户」的对应属性(比如用户ID、邮箱、显示名称),系统会自动填充这些字段,用户看不到但提交时会包含。
  • 支持与WooCommerce Memberships集成,能直接获取会员等级信息(需付费扩展,但基础识别用户足够)。

2. Ninja Forms(免费核心版)

  • 添加字段后,在「高级」设置中开启「从当前用户获取值」,选择对应的用户元数据(如用户名、邮箱),字段会自动填充且可设置为隐藏。
  • 操作直观,无需代码,适合非开发人员。

3. 自定义纯代码表单(最灵活)

如果你有开发能力,完全可以自己写一个极简表单,避免依赖插件:

// 处理表单提交(放在主题functions.php或自定义插件)
add_action( 'init', 'handle_event_suggestion_submission' );
function handle_event_suggestion_submission() {
    if ( isset( $_POST['submit_suggestion'] ) && is_user_logged_in() ) {
        // 验证并 sanitize 数据
        $suggestion = sanitize_textarea_field( $_POST['event_suggestion'] );
        $current_user = wp_get_current_user();
        
        // 保存到自定义帖子类型(需先注册)
        $post_args = array(
            'post_title' => '活动建议:' . wp_trim_words( $suggestion, 10 ),
            'post_content' => $suggestion,
            'post_status' => 'draft',
            'post_type' => 'event_suggestion',
            'post_author' => $current_user->ID
        );
        $post_id = wp_insert_post( $post_args );
        
        // 保存会员信息(可选)
        $memberships = wc_memberships_get_user_memberships( $current_user->ID );
        $membership_names = wp_list_pluck( $memberships, 'plan_name' );
        add_post_meta( $post_id, 'user_memberships', implode( ', ', $membership_names ) );
        
        // 跳转提示页
        wp_redirect( home_url( '/suggestion-thank-you/' ) );
        exit;
    }
}

表单HTML(放在页面模板或短代码中):

<?php if ( is_user_logged_in() ) : ?>
    <form method="post" class="event-suggestion-form">
        <textarea name="event_suggestion" placeholder="请描述你的活动建议..." required rows="5"></textarea>
        <button type="submit" name="submit_suggestion" class="button">提交建议</button>
    </form>
<?php else : ?>
    <p>请<a href="<?php echo wp_login_url( get_permalink() ); ?>">登录</a>后提交活动建议</p>
<?php endif; ?>

关键注意事项

  • 始终在表单页面添加登录检测,未登录用户不显示表单,避免匿名提交。
  • 所有用户提交的数据都要做sanitize处理,防止安全问题。
  • 若需区分会员等级,利用WooCommerce Memberships的wc_memberships_get_user_memberships()函数即可获取用户的会员计划信息。

内容的提问来源于stack exchange,提问作者daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:10:02