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

如何在WooCommerce我的账户中为管理员或客户账户添加验证徽章

在WooCommerce我的账户页面添加用户验证徽章的解决方案

一、代码实现(PHP + CSS)

1. 添加徽章标记(PHP)

将以下代码添加到主题的functions.php文件或自定义插件中,它会在已验证用户的显示名称后追加徽章元素:

add_filter( 'woocommerce_account_menu_user_name', 'add_verification_badge_to_account_name' );
function add_verification_badge_to_account_name( $name ) {
    $user = wp_get_current_user();
    // 通过用户meta字段判断是否已验证,需先给目标用户添加该字段值为"yes"
    $is_verified = get_user_meta( $user->ID, 'is_verified_user', true );
    
    if ( $is_verified === 'yes' ) {
        // 这里用文字徽章,也可替换为图片标签
        $name .= '<span class="user-verified-badge">✓</span>';
    }
    return $name;
}

如果需要使用自定义图片徽章,将上述代码中的<span>标签替换为:

<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/verified-badge.png" alt="已验证" class="user-verified-badge">

(需将/images/verified-badge.png替换为你实际的徽章图片路径)

2. 美化徽章样式(CSS)

将以下CSS添加到主题自定义CSS面板或style.css文件中,可根据网站风格调整样式:

.user-verified-badge {
    display: inline-block;
    margin-left: 6px;
    padding: 2px 6px;
    background: #28a745;
    color: #fff;
    border-radius: 50%;
    font-size: 12px;
    font-weight: bold;
    line-height: 1;
}
/* 图片徽章样式 */
.user-verified-badge img {
    width: 16px;
    height: 16px;
    vertical-align: middle;
    margin-left: 6px;
}

3. 后台添加验证标记(可选)

如果需要在WordPress后台手动标记验证用户,添加以下代码来在用户编辑页面添加勾选框:

// 显示验证字段
add_action( 'show_user_profile', 'add_verified_field_to_profile' );
add_action( 'edit_user_profile', 'add_verified_field_to_profile' );
function add_verified_field_to_profile( $user ) {
    ?>
    <h3>用户验证设置</h3>
    <table class="form-table">
        <tr>
            <th><label for="is_verified_user">是否已验证</label></th>
            <td>
                <input type="checkbox" name="is_verified_user" id="is_verified_user" value="yes" <?php checked( 'yes', get_user_meta( $user->ID, 'is_verified_user', true ) ); ?> />
                <span class="description">勾选后该用户账户名称旁将显示验证徽章</span>
            </td>
        </tr>
    </table>
    <?php
}

// 保存验证字段
add_action( 'personal_options_update', 'save_verified_field' );
add_action( 'edit_user_profile_update', 'save_verified_field' );
function save_verified_field( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) ) return;
    update_user_meta( $user_id, 'is_verified_user', isset( $_POST['is_verified_user'] ) ? 'yes' : 'no' );
}

二、插件方案(无需代码)

  • User Verification for WooCommerce:支持邮箱/手机号验证,验证通过后可通过插件自带的用户标记,结合自定义CSS或插件钩子将徽章添加到账户名称旁。
  • Profile Builder:可自定义用户字段,添加“已验证”字段后,通过插件显示钩子或简单代码实现徽章展示,同时支持批量管理用户验证状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 21:01:05