2

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

Patterns menu/page

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.

4
  • What about remove_theme_support( 'core-block-patterns' ); ?
    – birgire
    Commented Apr 17 at 16:07
  • @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 at 20:46
  • Maybe adjust hooks´s priorities?
    – birgire
    Commented Apr 22 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 at 10:49

2 Answers 2

1

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' );
}
1
  • Tested and confirmed this works for me as well! Note that this only hides the menu item, it doesn't remove the capability. If a user enters the URL manually in their address bar, they can still access the Patterns page. Depending on your use case, this may be sufficient or you might need a different solution.
    – doofusb0y
    Commented Apr 26 at 13:17
0

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'.

5
  • mental -1, just because you don't see something do not mean it is not there and do not have surprising side effects, or will not have in the future. The accepted answer also iffy in this way. Maybe what you should look at is the classic editor plugin if it is still maintained. Commented Apr 26 at 14:54
  • @MarkKaplun Yep, the classic editor plugin is still maintained and I run that on all my sites. It unfortunately doesn't interact with the Patterns menu/page.
    – doofusb0y
    Commented Apr 29 at 14:55
  • maybe worth to open a ticket for that? not much point in having patterns when you disable the other aspects of gutenberg (and i hope the classic editor disable the site editor related functionality as well) Commented Apr 29 at 15:50
  • @MarkKaplun I completely agree. Unfortunately, they've said "it's a feature, not a bug": wordpress.org/support/topic/patterns-menu-item At least as far as the Classic Editor goes. And the fact that the code hooks to disable Gutenberg don't affect the Patterns menu item either.
    – doofusb0y
    Commented May 2 at 19:43
  • :( yeh I predicted that there would be a time after which there will not be a good way to avoid the gutenberg BS, still sad to see it coming. Commented May 3 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.