WordPress 个人档案(用户资料)是每个注册用户在网站上的身份信息集合。以下是管理 WordPress 个人档案的完整指南:

一、访问个人档案

1. 后台访问方式

  1. 登录 WordPress 后台

  2. 点击右上角管理员名称 → "编辑我的个人资料"

  3. 或直接访问:/wp-admin/profile.php

2. 前端访问方式(需插件或主题支持)

  • 使用短代码:[user_profile]

  • 专用页面:/profile/ 或 /account/

二、默认个人档案字段

1. 基本个人信息

  • 用户名:不可修改(需插件支持修改)

  • :用户全名

  • 昵称:显示名称的基础

  • 显示名称公开为:选择前端显示的名称形式

2. 联系信息

  • 电子邮箱:必须唯一且有效

  • 网站:个人主页或公司网站

3. 账户管理

  • 新密码:可在此修改密码

  • 确认密码:重复新密码

4. 个人选项

  • 可视化编辑器:是否使用富文本编辑器

  • 语法高亮:代码编辑时是否启用高亮

  • 工具栏:前端是否显示管理员工具栏

三、扩展个人档案字段

1. 使用插件添加字段

推荐插件:

  • Advanced Custom Fields (ACF):专业字段管理

  • User Profile Builder:可视化表单构建

  • BuddyPress:社交网络风格档案

2. 代码添加自定义字段

// 在 functions.php 中添加
add_action('show_user_profile', 'add_custom_profile_fields');
add_action('edit_user_profile', 'add_custom_profile_fields');
function add_custom_profile_fields($user) {
    ?>
    <h3>额外信息</h3>
    <table class="form-table">
        <tr>
            <th><label for="phone">手机号码</label></th>
            <td>
                <input type="tel" name="phone" id="phone" 
                       value="<?php echo esc_attr(get_user_meta($user->ID, 'phone', true)); ?>">
                <p class="description">请输入您的联系电话</p>
            </td>
        </tr>
        <tr>
            <th><label for="bio">个人简介</label></th>
            <td>
                <textarea name="bio" id="bio" rows="5"><?php echo esc_textarea(get_user_meta($user->ID, 'bio', true)); ?></textarea>
            </td>
        </tr>
    </table>
    <?php
}

// 保存字段
add_action('personal_options_update', 'save_custom_profile_fields');
add_action('edit_user_profile_update', 'save_custom_profile_fields');
function save_custom_profile_fields($user_id) {
    if (!current_user_can('edit_user', $user_id)) return false;
    
    update_user_meta($user_id, 'phone', sanitize_text_field($_POST['phone']));
    update_user_meta($user_id, 'bio', sanitize_textarea_field($_POST['bio']));
}

四、前端显示个人档案

1. 使用短代码显示

// 创建短代码
add_shortcode('display_user_profile', 'display_user_profile_func');
function display_user_profile_func() {
    if (!is_user_logged_in()) return '请登录查看个人资料';
    
    $user = wp_get_current_user();
    ob_start();
    ?>
    <div class="user-profile">
        <div class="avatar"><?php echo get_avatar($user->ID, 120); ?></div>
        <h2><?php echo esc_html($user->display_name); ?></h2>
        <p><strong>邮箱:</strong><?php echo esc_html($user->user_email); ?></p>
        <?php if ($phone = get_user_meta($user->ID, 'phone', true)): ?>
            <p><strong>电话:</strong><?php echo esc_html($phone); ?></p>
        <?php endif; ?>
        <?php if ($bio = get_user_meta($user->ID, 'bio', true)): ?>
            <div class="bio"><?php echo wpautop(esc_textarea($bio)); ?></div>
        <?php endif; ?>
    </div>
    <?php
    return ob_get_clean();
}

2. 在主题模板中显示

// 在 author.php 或自定义模板中
$user = get_queried_object();
$social_links = array(
    'twitter' => get_user_meta($user->ID, 'twitter', true),
    'facebook' => get_user_meta($user->ID, 'facebook', true),
    'linkedin' => get_user_meta($user->ID, 'linkedin', true)
);
?>
<article class="user-profile">
    <header>
        <?php echo get_avatar($user->ID, 200); ?>
        <h1><?php echo esc_html($user->display_name); ?></h1>
        <p class="user-title"><?php echo esc_html(get_user_meta($user->ID, 'title', true)); ?></p>
    </header>
    
    <div class="user-bio">
        <?php echo wpautop(esc_textarea(get_user_meta($user->ID, 'description', true))); ?>
    </div>
    
    <?php if (array_filter($social_links)): ?>
    <div class="user-social">
        <h3>社交链接</h3>
        <ul>
            <?php foreach ($social_links as $network => $url): 
                if (!empty($url)): ?>
                <li>
                    <a href="<?php echo esc_url($url); ?>" target="_blank">
                        <?php echo esc_html(ucfirst($network)); ?>
                    </a>
                </li>
                <?php endif;
            endforeach; ?>
        </ul>
    </div>
    <?php endif; ?>
</article>

五、个人档案安全设置

1. 隐私保护

// 只允许用户查看自己的完整资料
add_action('template_redirect', 'protect_user_profiles');
function protect_user_profiles() {
    if (is_author() && !current_user_can('edit_users')) {
        $queried_user = get_queried_object();
        if (get_current_user_id() != $queried_user->ID) {
            wp_redirect(home_url());
            exit;
        }
    }
}

2. 敏感字段保护

// 加密存储敏感信息
add_filter('update_user_metadata', 'encrypt_sensitive_data', 10, 5);
function encrypt_sensitive_data($null, $user_id, $meta_key, $meta_value, $prev_value) {
    $sensitive_fields = array('id_number', 'bank_account');
    
    if (in_array($meta_key, $sensitive_fields)) {
        $encrypted = openssl_encrypt($meta_value, 'AES-256-CBC', AUTH_KEY);
        update_user_meta($user_id, $meta_key.'_encrypted', $encrypted);
        return true; // 阻止原始值保存
    }
    
    return $null;
}

六、个人档案样式优化

1. CSS 样式示例

/* 个人资料页基础样式 */
.user-profile {
    max-width: 800px;
    margin: 2em auto;
    padding: 2em;
    background: #fff;
    box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

.user-profile header {
    text-align: center;
    margin-bottom: 2em;
}

.user-profile .avatar {
    border-radius: 50%;
    overflow: hidden;
    width: 120px;
    height: 120px;
    margin: 0 auto 1em;
}

.user-social ul {
    list-style: none;
    padding: 0;
    display: flex;
    gap: 1em;
}

.user-social a {
    text-decoration: none;
    padding: 0.5em 1em;
    background: #f0f0f0;
    border-radius: 4px;
}

/* 后台个人资料页调整 */
#profile-page h2 {
    color: #2271b1;
    border-bottom: 1px solid #ddd;
    padding-bottom: 0.5em;
}

2. 响应式设计

@media (max-width: 600px) {
    .user-profile {
        padding: 1em;
    }
    
    .user-social ul {
        flex-direction: column;
    }
}

七、个人档案相关插件推荐

  1. Advanced Custom Fields - 创建复杂的自定义字段

  2. BuddyPress - 完整的社交网络档案系统

  3. User Role Editor - 精细控制字段访问权限

  4. WPForms - 创建前端个人资料编辑表单

  5. ProfileGrid - 用户目录和高级档案

八、常见问题解决

1. 头像不显示

  • 确认用户已设置 Gravatar 或本地头像

  • 检查 get_avatar() 函数是否正确使用

  • 查看控制台是否有加载错误

2. 自定义字段不保存

  • 检查字段名称是否冲突

  • 验证 update_user_meta 是否正确调用

  • 确保用户有编辑权限

3. 前端表单安全问题

  • 所有输出必须使用 esc_html() 等转义函数

  • 使用 nonce 验证表单提交

  • 实施能力检查 current_user_can()

通过以上方法,您可以全面管理 WordPress 个人档案功能,创建符合网站需求的用户资料系统。