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

WordPress:如何通过ACF实现用户关注功能并更新ACF用户字段

解决方案:用ACF实现用户关注功能(含前端AJAX更新)

问题分析

你原代码的核心问题:

  • acf/save_post 钩子是针对文章/自定义帖子类型的,用户资料不属于post类型,所以这个钩子根本不会触发
  • 错误使用get_post_type($current_user),acf/save_post的参数是帖子ID,不是用户对象
  • ACF用户字段(对象类型)需要存储用户ID,不是user_nicename
  • 单独的提交按钮没有绑定正确的处理逻辑,也没有表单或AJAX触发后端操作

完整实现步骤

1. 前端:在用户资料页添加关注/取消关注按钮

在你的用户资料模板(比如author.php或者自定义模板)中插入以下代码:

<?php
// 获取当前访问的目标用户ID
$target_user_id = get_queried_object_id();
// 获取当前登录用户ID
$current_user_id = get_current_user_id();

// 跳过未登录用户和用户关注自己的情况
if (!$current_user_id || $current_user_id == $target_user_id) {
    return;
}

// 获取当前用户已关注的用户列表(替换成你的ACF字段键)
$followed_users = get_field('field_63d0017ab088f', 'user_' . $current_user_id);
// 处理空值,确保是数组
$followed_users = is_array($followed_users) ? $followed_users : [];

// 判断是否已关注
$is_following = in_array($target_user_id, $followed_users);
?>

<button class="follow-user-btn" data-target-user="<?php echo $target_user_id; ?>">
    <?php echo $is_following ? '取消关注' : '关注'; ?>
</button>

<script>
jQuery(document).ready(function($) {
    $('.follow-user-btn').on('click', function(e) {
        e.preventDefault();
        const $btn = $(this);
        const targetUserId = $btn.data('target-user');

        // 发送AJAX请求
        $.ajax({
            url: '<?php echo admin_url('admin-ajax.php'); ?>',
            type: 'POST',
            data: {
                action: 'toggle_user_follow',
                target_user_id: targetUserId,
                nonce: '<?php echo wp_create_nonce('follow_user_nonce'); ?>'
            },
            success: function(response) {
                if (response.success) {
                    // 切换按钮文本
                    $btn.text(response.data.is_following ? '取消关注' : '关注');
                } else {
                    alert('操作失败,请重试');
                }
            },
            error: function() {
                alert('网络错误,请重试');
            }
        });
    });
});
</script>

2. 后端:添加AJAX处理函数

在主题的functions.php中插入以下代码:

// 处理关注/取消关注的AJAX请求
add_action('wp_ajax_toggle_user_follow', 'toggle_user_follow');
add_action('wp_ajax_nopriv_toggle_user_follow', 'toggle_user_follow');

function toggle_user_follow() {
    // 验证权限和nonce
    if (!is_user_logged_in() || !wp_verify_nonce($_POST['nonce'], 'follow_user_nonce')) {
        wp_send_json_error('无权操作');
    }

    $current_user_id = get_current_user_id();
    $target_user_id = intval($_POST['target_user_id']);

    // 禁止关注自己
    if ($current_user_id == $target_user_id) {
        wp_send_json_error('不能关注自己');
    }

    // 获取ACF字段(替换成你的字段键)
    $field_key = 'field_63d0017ab088f';
    $followed_users = get_field($field_key, 'user_' . $current_user_id);
    $followed_users = is_array($followed_users) ? $followed_users : [];

    // 切换关注状态
    if (in_array($target_user_id, $followed_users)) {
        // 取消关注:移除目标用户ID
        $followed_users = array_diff($followed_users, [$target_user_id]);
        $is_following = false;
    } else {
        // 关注:添加目标用户ID
        $followed_users[] = $target_user_id;
        $is_following = true;
    }

    // 更新ACF字段
    update_field($field_key, $followed_users, 'user_' . $current_user_id);

    // 返回结果
    wp_send_json_success([
        'is_following' => $is_following
    ]);
}

关键注意事项

  • 确保你的ACF用户字段设置为允许多选(因为要存储多个关注用户ID)
  • 替换代码中的field_63d0017ab088f为你实际的ACF字段键
  • 按钮样式可以根据你的主题自定义,添加CSS类即可
  • 测试时要确保用户已登录,且操作的是其他用户的资料页

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 11:40:24