This guide shows you how to hide specific blocks in the WordPress block editor (Gutenberg) so they only appear for certain user roles, like administrators or editors. You’ll use a small PHP snippet in your theme’s functions.php file or a code snippet plugin. No premium plugins are required, and the method works with any block, including core paragraphs, images, and custom blocks.
What You’ll Need
- A WordPress site where you can edit theme files or add code snippets (e.g., via a child theme or a plugin like Code Snippets).
- FTP or file manager access if you plan to edit functions.php directly.
- Basic understanding of PHP and WordPress user roles (Administrator, Editor, Author, Contributor, Subscriber).
- A staging or test site is recommended to avoid breaking your live site.
Step-by-Step
- Identify the block you want to hide. In the editor, click on the block. In the right sidebar, under the block’s name, you’ll see the block type (e.g., “Paragraph”, “Image”, “Latest Posts”). For custom blocks, it may look like “plugin-name/block-name”. Write this down.
- Decide which user roles should see the block. For example, you might want only administrators to see a “Call to Action” block, or hide a “Testimonials” block from contributors.
- Add the PHP snippet. Go to Appearance → Theme File Editor, open functions.php, or use a code snippet plugin. Add the following code, replacing the placeholder block name and role with your own:
-
function hide_block_for_non_admins( $allowed_blocks, $post ) { if ( ! current_user_can( 'administrator' ) ) { // Remove the block from the allowed list $allowed_blocks = array_diff( $allowed_blocks, array( 'core/paragraph' ) ); } return $allowed_blocks; } add_filter( 'allowed_block_types_all', 'hide_block_for_non_admins', 10, 2 ); - Save the changes. If you edited functions.php, save the file. If using a snippet plugin, activate the snippet.
- Test with different user accounts. Log in as a non-admin user (e.g., an editor) and open the editor. The block you specified should not appear in the block inserter. If you want to hide it from the block list but still allow it to be used if already in content, you may need a different approach (see Tips).
Common Mistakes
- Using the wrong block name. The block name is not the display name. For example, the paragraph block is ‘core/paragraph’, not ‘paragraph’. Use the browser inspector or the block’s panel to confirm.
- Forgetting to return the array. The filter must return the modified array. If you don’t, all blocks will be hidden.
- Using current_user_can with a role that doesn’t exist. Roles like ‘admin’ are not correct; use ‘administrator’.
- Editing the parent theme directly. If you update the theme, your changes will be lost. Always use a child theme or a snippet plugin.
- Assuming the filter hides blocks already in content. This filter only removes blocks from the inserter; it does not remove blocks that are already placed in a post. For that, you’d need a more advanced script.
Tips
- To hide multiple blocks, use an array of block names:
array( 'core/paragraph', 'core/image' ). - To allow only certain blocks for a role, use
allowed_block_types_allto return a list of allowed blocks instead of removing from the existing list. - If you want to hide a block from the inserter but still allow it to be used if already in content, you can use the
block_editor_settings_allfilter to modify theallowedBlockTypessetting. This is more complex and requires JavaScript. - Use a code snippet plugin like Code Snippets to avoid editing theme files. It’s safer and easier to manage.
- Test on a staging site first to ensure you don’t lock yourself out of the editor.
Troubleshooting
All blocks are gone. This usually means the filter is returning an empty array. Check that you are returning the modified array and that your condition is correct.
The block still shows for non-admins. Make sure you are using the correct block name. Also, check if a caching plugin is caching the editor scripts. Clear the cache.
The filter doesn’t work with custom blocks. Some custom blocks may not be registered with the standard ‘namespace/block’ format. Check the block’s source code or documentation.
Changes don’t take effect. WordPress caches the allowed blocks list. Try clearing the browser cache and any server-side caching plugin.
The editor breaks. If you see a white screen or JavaScript errors, remove the snippet immediately. Check your PHP syntax; a missing semicolon or bracket can cause a fatal error.
Frequently Asked Questions
Can I hide blocks from specific users, not just roles?
Yes, you can use current_user_can() with capabilities, or check the user ID with get_current_user_id(). For example, if ( get_current_user_id() === 5 ).
Will this hide the block from the block list in the editor?
Yes, the filter removes the block from the allowed block types, so it won’t appear in the inserter. However, if the block is already in the content, it will still be visible and editable.
Can I hide blocks based on user meta or custom conditions?
Absolutely. You can use any PHP condition inside the filter, such as checking user meta with get_user_meta() or a custom capability.
Does this work with the classic editor?
No, this filter is specific to the block editor (Gutenberg). For the classic editor, you would need a different approach, like hiding meta boxes.
Will this affect the front-end display?
No, this filter only affects the editor. The front-end will still display the block if it’s in the content. To hide on the front-end, you’d need to modify the content output.
Is there a plugin that does this without code?
Yes, there are plugins like “Block Visibility” or “Lazy Blocks” that provide a UI for controlling block visibility based on user roles, but they may have additional features and overhead. The code method is lightweight and free.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Turn Off Backup Encryption in iTunes?
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It