Hide Toolbar Links inside the WordPress Block Editor

Over the last couple of years, more and more WordPress users have adopted the Gutenberg block editor. We use it for all the PublishPress sites, and an increasing number of support tickets are about Gutenberg.

We had an interesting support ticket recently: a customer wanted to hide links in the menu that is attached to each block. You can see a screenshot of this menu below.

Create pattern link

In this guide, I'll show you how to remove items from this toolbar. We'll be hiding items for specific user roles. In this example, we'll start by hiding the “Create pattern” link and icon in the Gutenberg toolbar menu.

This guide uses the Editor Features option in the PublishPress Capabilities Pro plugin.


How to hide “Create pattern”

  • Go to “Capabilities”, then “Editor Features”.
  • In the top left corner, choose the user role you want to hide the link for. You can choose “Editor”, “Author”, or any other WordPress role.
  • Scroll down to the “Custom Gutenberg Restrictions” area at the bottom of the screen.
  • Enter the following code into the “Element IDs or Classes” box.
.components-popover.components-dropdown__content.block-editor-block-settings-menu__popover.components-dropdown-menu__popover .components-popover__content .components-dropdown-menu__menu div.components-menu-group:nth-child(3) div .components-button.components-menu-item__button:nth-child(4)
Hide the create pattern link
  • Click “Add New”.
  • Click “Save Post Restrictions”.
  • Now when you visit a post, the “Create pattern” link will be missing, as in this screenshot:

How this code works

This code works using the ID numbers of the menu. There are two numbers involved here. The first number refers to the grouping. There are four groups in this dropdown menu.

  1. Copy / Cut / Duplicate / Add before / Add after
  2. Copy styles / Paste styles
  3. Group / Local / Rename / Create pattern / Edit as HTML
  4. Delete

And then there are numbers inside each group. For example, there are the numbers inside the third group:

  1. Group
  2. Lock
  3. Rename
  4. Create pattern
  5. Edit as HTML

So this is why use “3” and “4” to target the “Create pattern” link. It is in the third group and is the fourth link.

ID numbers for Gutenberg menus

Let's see the code for these menu links

  1. Group > .components-button.components-menu-item__button:nth-child(1)
  2. Lock > .components-button.components-menu-item__button:nth-child(2)
  3. Rename > .components-button.components-menu-item__button:nth-child(3)
  4. Create pattern > .components-button.components-menu-item__button:nth-child(4)
  5. Edit as HTML > .components-button.components-menu-item__button:nth-child(5)

You can adjust this code to remove other items in the screenshot. This code uses the numbers to target different elements.

For example, change both numbers to “1” and you'll be able to hide the “Copy” link:

.components-popover.components-dropdown__content.block-editor-block-settings-menu__popover.components-dropdown-menu__popover .components-popover__content .components-dropdown-menu__menu div.components-menu-group:nth-child(1) div .components-button.components-menu-item__button:nth-child(1)

Here's another example: change both numbers to “2” and you'll be able to hide the “Paste styles” link:

.components-popover.components-dropdown__content.block-editor-block-settings-menu__popover.components-dropdown-menu__popover .components-popover__content .components-dropdown-menu__menu div.components-menu-group:nth-child(2) div .components-button.components-menu-item__button:nth-child(2)

And one file example: change the first number to “4” and the second number to “1”. This will hide the “Delete” link:

.components-popover.components-dropdown__content.block-editor-block-settings-menu__popover.components-dropdown-menu__popover .components-popover__content .components-dropdown-menu__menu div.components-menu-group:nth-child(4) div .components-button.components-menu-item__button:nth-child(1)

More about Controlling Access to Gutenberg

The PublishPress plugins offer a wide variety of ways to control the Gutenberg editor. For example, there are ways to disable the Gutenberg editor for specific roles. You can also hide specific blocks, or hide metaboxes from 3rd party plugins.

PublishPress Capabilities icon
  • Steve is the founder of PublishPress. He's been working with open source software for over 20 years. Originally from the UK, he now lives in Sarasota in the USA. This profile is generated by the PublishPress Authors plugin.

Leave a Reply

Your email address will not be published. Required fields are marked *