admin管理员组

文章数量:1122832

Not sure quite when, but a recent WordPress release has added a new WP Admin link under Appearance for Patterns:

My themes don't use Gutenberg, and I've disabled it. But the Patterns screen is still accessible. Not a big deal for me, I can just ignore it. But it's confusing to my clients. They have Editor level access, which currently does have access to this Patterns screen.

I normally disable capabilities my clients shouldn't or don't need access to with functions.php code such as:

/* Customizing 'Editor' users
--------------------------------------------------*/
function set_editor_capabilities() {

    // Get the Editor role
    $editor = get_role( 'editor' );
    
    // List of capabilities to add
    $caps = array(
        
        // Appearance
        'edit_theme_options',
        
        // Users
        'create_users',
        'edit_users',
        'promote_users',
        'remove_users',
        'list_users',
        'delete_users'
    );
    
    // Add the capabilities
    foreach ( $caps as $cap ) {
        $editor->add_cap( $cap );
    }
    
    // List of capabilities to remove
    $caps = array(
    
        // Links
        'manage_links',
        
        // Tools
        'import'
    );
    
    // Remove the capabilities
    foreach ( $caps as $cap ) {
        $editor->remove_cap( $cap );
    }
}
add_action( 'init', 'set_editor_capabilities' );

Another method I've used if I can't disable the capability is to at least remove the link from the menu so they're unlikely to stumble across it:

/* Menus
--------------------------------------------------*/
function custom_remove_menus(){
    if(!current_user_can('administrator')){
        
        // Comments
        remove_menu_page('edit-comments.php');
                
        // Settings
        remove_menu_page('options-general.php');
        
        // Appearance
        remove_submenu_page('themes.php', 'themes.php');
        remove_submenu_page('themes.php', 'widgets.php');

        // Tools
        remove_menu_page('tools.php');
        
    }
}
add_action('admin_menu', 'custom_remove_menus');

Unfortunately, none of these tricks are doing the job, and I can't find any information on this online (probably due to the newness of the feature).

Imperfect solutions:

  • I think I could do a sneaky CSS solution to target and hide just that Patterns link.
  • Admin plugins like Adminimize can restrict users from accessing Patterns, even if you type in the URL directly, but I'd prefer a non-plugin solution and not add a dependency.

Hoping there's a better and cleaner permissions solution out there. Desired solutions would be, in order of preference:

  • Disable Patterns feature entirely for all users
  • Remove Patterns capability from Editor users
  • Hide Patterns menu link from Editor users

Hopefully we can find a solution that will help other folks in the future!


Update #1: I've discovered that the Patterns link seems to tie into the edit_theme_options capability, as do the other items under the Appearance menu. So my challenge is actually that I want to disable Editor access to all these items except for Menus, which is tricky since they're both tied to the same capability.

Not sure quite when, but a recent WordPress release has added a new WP Admin link under Appearance for Patterns:

My themes don't use Gutenberg, and I've disabled it. But the Patterns screen is still accessible. Not a big deal for me, I can just ignore it. But it's confusing to my clients. They have Editor level access, which currently does have access to this Patterns screen.

I normally disable capabilities my clients shouldn't or don't need access to with functions.php code such as:

/* Customizing 'Editor' users
--------------------------------------------------*/
function set_editor_capabilities() {

    // Get the Editor role
    $editor = get_role( 'editor' );
    
    // List of capabilities to add
    $caps = array(
        
        // Appearance
        'edit_theme_options',
        
        // Users
        'create_users',
        'edit_users',
        'promote_users',
        'remove_users',
        'list_users',
        'delete_users'
    );
    
    // Add the capabilities
    foreach ( $caps as $cap ) {
        $editor->add_cap( $cap );
    }
    
    // List of capabilities to remove
    $caps = array(
    
        // Links
        'manage_links',
        
        // Tools
        'import'
    );
    
    // Remove the capabilities
    foreach ( $caps as $cap ) {
        $editor->remove_cap( $cap );
    }
}
add_action( 'init', 'set_editor_capabilities' );

Another method I've used if I can't disable the capability is to at least remove the link from the menu so they're unlikely to stumble across it:

/* Menus
--------------------------------------------------*/
function custom_remove_menus(){
    if(!current_user_can('administrator')){
        
        // Comments
        remove_menu_page('edit-comments.php');
                
        // Settings
        remove_menu_page('options-general.php');
        
        // Appearance
        remove_submenu_page('themes.php', 'themes.php');
        remove_submenu_page('themes.php', 'widgets.php');

        // Tools
        remove_menu_page('tools.php');
        
    }
}
add_action('admin_menu', 'custom_remove_menus');

Unfortunately, none of these tricks are doing the job, and I can't find any information on this online (probably due to the newness of the feature).

Imperfect solutions:

  • I think I could do a sneaky CSS solution to target and hide just that Patterns link.
  • Admin plugins like Adminimize can restrict users from accessing Patterns, even if you type in the URL directly, but I'd prefer a non-plugin solution and not add a dependency.

Hoping there's a better and cleaner permissions solution out there. Desired solutions would be, in order of preference:

  • Disable Patterns feature entirely for all users
  • Remove Patterns capability from Editor users
  • Hide Patterns menu link from Editor users

Hopefully we can find a solution that will help other folks in the future!


Update #1: I've discovered that the Patterns link seems to tie into the edit_theme_options capability, as do the other items under the Appearance menu. So my challenge is actually that I want to disable Editor access to all these items except for Menus, which is tricky since they're both tied to the same capability.

Share Improve this question edited Apr 17, 2024 at 14:59 doofusb0y asked Apr 17, 2024 at 14:13 doofusb0ydoofusb0y 355 bronze badges 4
  • What about remove_theme_support( 'core-block-patterns' ); ? – birgire Commented Apr 17, 2024 at 16:07
  • 1 @birgire Sadly no! I tried that and the other techniques here (wpexplorer.com/…) but none of them impacted the Patterns sidebar menu link. – doofusb0y Commented Apr 17, 2024 at 20:46
  • Maybe adjust hooks´s priorities? – birgire Commented Apr 22, 2024 at 11:32
  • I'm also looking for a solution to this - I use "User role editor" and can easily hide the menu item via the plugin, however I have 300 websites which are all clones of each other so looking for a function that I can just deploy via a theme update rather than have to log in to 300 websites and update manually. – Adrian Commented Apr 24, 2024 at 10:49
Add a comment  | 

5 Answers 5

Reset to default 2

This works for me:

// Remove WP Block
add_action('admin_init', 'remove_wp_block_menu', 100);
function remove_wp_block_menu() {
    remove_submenu_page( 'themes.php', 'edit.php?post_type=wp_block' );
}

I hope this would be helpful.

function remove_menus_appearance_patterns(){  
    // Appearance > Patterns  
    remove_submenu_page('themes.php', 'site-editor.php?path=/patterns');  
}  
add_action('admin_menu', 'remove_menus_appearance_patterns');

This code worked to remove patterns from the admin menu:

add_action('admin_init', 'remove_wp_block_menu', 100);
function remove_wp_block_menu() {
  remove_submenu_page( 'themes.php', 'site-editor.php?path=/patterns' );
}

user242338 posted a solid answer that does hide the menu item, so I've selected this as the accepted answer for now.

However, the solution I ended up going with was the Admin Menu Editor plugin. I was hoping to avoid a plugin, but this one was able to solve all the problems for my particular use case, and some others that weren't part of my initial question:

  • Plugin has a good install base and update/support history
  • Free version is sufficient for my needs, with inexpensive paid version available with additional features
  • Removing a menu item also disables direct access so users can't circumvent it by manually typing in the URL
  • As a bonus, you can also rearrange and customize menu items. This allowed me to reorder the menu in order of importance for my clients (Pages, Posts, Media, etc).

I may circle back to hardcoded ways to accomplish all this in PHP at a later date, but it's frustrating how difficult WordPress makes it to customize the admin. I got everything I wanted and more out of the plugin in half an hour and it was an easily understandable process, compared to the hours I spent banging my head on PHP functions that only gave me partial solutions. IMO some version of this plugin should be native functionality!

Hope this question helps somebody else with their own WP Admin wrasslin'.

add_action( 'admin_menu',  function () {      
         
        remove_submenu_page( 'themes.php', 'edit.php?post_type=wp_block' );
        remove_submenu_page( 'themes.php', 'site-editor.php?path=/patterns' );        
    
});

本文标签: functionsRemove or disable Appearance gt Patterns