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

如何在WordPress后台为自定义用户隐藏指定高级自定义字段(ACF)?

How to Hide Specific ACF Fields for Custom User Roles in WP-Admin

Hey there! Hiding specific Advanced Custom Fields (ACF) for custom user roles in the WordPress admin is a common requirement, and there are a couple of straightforward ways to pull this off—let's break them down:

Method 1: Use ACF's Built-in Visibility Settings (No Code Needed)

If you want a quick, code-free solution (great for hiding entire field groups), ACF has built-in role-based visibility controls:

  • Go to Custom Fields > Field Groups and edit the field group you want to restrict.
  • Switch to the Settings tab (next to Location).
  • Scroll down to the Visibility section.
  • Set the dropdown to Role, then choose either:
    • Show this field group if > Role > is not equal to your custom user role, or
    • Show this field group if > Role > is equal to the roles you want to allow access.
  • Save the field group. Now users with your custom role won’t see that field group in the admin.

Method 2: Use ACF Hooks for Granular Control (Hide Individual Fields)

If you need to hide a single field (not an entire group) or have more complex conditions, use ACF's acf/prepare_field filter. Add this code to your theme’s functions.php file or a custom plugin:

add_filter('acf/prepare_field', 'hide_specific_acf_field_for_custom_role');

function hide_specific_acf_field_for_custom_role($field) {
    // Replace with your custom user role slug (e.g., 'editor', 'custom_author')
    $restricted_role = 'custom_user_role';
    // Replace with the key of the ACF field you want to hide (find this in the field's edit URL)
    $target_field_key = 'field_1234567890abc';
    
    $current_user = wp_get_current_user();
    
    // Check if the current user has the restricted role and we're targeting the right field
    if (in_array($restricted_role, $current_user->roles) && $field['key'] === $target_field_key) {
        return false; // Return false to hide the field
    }
    
    return $field; // Return the field normally for other users/fields
}

Bonus: Hide an Entire Field Group with Hooks

If you prefer using code for field groups instead of the UI, use the acf/load_field_group filter:

add_filter('acf/load_field_group', 'hide_acf_field_group_for_custom_role');

function hide_acf_field_group_for_custom_role($field_group) {
    $restricted_role = 'custom_user_role';
    // Replace with your field group's key (find this in the field group's edit URL)
    $target_group_key = 'group_1234567890abc';
    
    $current_user = wp_get_current_user();
    
    if (in_array($restricted_role, $current_user->roles) && $field_group['key'] === $target_group_key) {
        return false;
    }
    
    return $field_group;
}

Important Notes

  • Double-check your role slug and field/group keys—you can find the field/group key in the URL when editing the field/group, or in the field/group settings panel.
  • To avoid losing code when updating your theme, use a custom plugin to store these snippets instead of functions.php.
  • Always test by switching to the custom user role to confirm the fields are hidden as expected.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:36:32