• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Startup Hero

The Official Blog Of Name Hero

Learn To Fly Above The Competition Get Started
  • HomeWelcome
  • CoursesVideo Training Center
  • About UsWhat is StartupHero?
  • BlogGet the latest
  • Start HereStartup 101
  • SpeakingPodcast & Media
  • ResourcesTools to help You
    • Reselling WordPress Hosting
    • Resell Hero
    • How To Start A Blog
  • NameHeroCloud Web Hosting

How To Limit The Selection Of Blocks In Gutenberg

By Bhagwad Park on May 27, 2019 0

How To Limit The Selection Of Blocks In Gutenberg

When Gutenberg came out, it brought with it, a vastly expanded selection of default content types, we call “blocks”. Previously in the TinyMCE editor, we had buttons at the top, each of which represented a different type of content – headings, lists, and those inserted by plugins. In time though, the developers felt that this was too restrictive, and didn’t accurately represent the way pages are built on the web today.

Gutenberg has a dizzying selection of blocks that we can use. For example, here’s just the starting selection:

Before you Limit the Blocks in Gutenberg WordPress
Lots of Gutenberg Blocks

Some of these like the “Heading” block, have sub-choices that allow us to specify the flavor. For example, we can choose h1, h2, or h3 headings. This works great if you’re the sole editor of your website, and know exactly what to use, and what not to.

How to Prevent Accidental Block Usage

However, if you have a bunch of people contributing to your site, you might want to restrict their access to the wide variety of blocks. A simple reason could be to reduce confusion and streamline the editing process. Another could be that you don’t want to reveal too much of the inner workings of your website to 3rd party authors.

Some blocks can expose table names, or other internal codes used on your site that you want to keep private. So it’s perfectly understandable if you need to limit the blocks that are available by default. In the code below, I’ll show you how to create a “safe” list of blocks for your users, and you can customize in on your own.

Limiting the Number of Available Blocks

Let’s say we want to limit the number of available blocks to the following list:

  • Images
  • Paragraphs
  • Headings
  • Lists
  • Re-usable blocks

To implement this, paste the following code into your functions.php, or the place where you store your custom PHP code. If you’re new to pasting code in WordPress, here’s a tutorial from our knowledge base on how to do just that.

function restrict_blocks( $allowed_blocks, $post ) {
     $allowed_blocks = array(
         'core/block',
         'core/image',
         'core/paragraph',
         'core/heading',
         'core/list'
     );
     return $allowed_blocks;
 }
 add_filter( 'allowed_block_types', 'restrict_blocks');

Once you save your changes and load a new WordPress editing page, you should see the following when you try and insert a new block:

Limited Selection of Gutenberg Blocks
Restrict the number of Gutenberg Blocks

As you can see, the number is greatly restricted. I don’t have any re-usable blocks set up, so it doesn’t show any.

Restricting the Blocks Based on User Role or Page Type

Of course, you don’t want to implement the above code “as is”, since it’ll restrict the blocks for everyone, including yourself. What you want to do, is first check to see if the user is in a particular role. For example, you might want to restrict roles only for Contributors. In which case, the code becomes:

function restrict_blocks( $allowed_blocks, $post ) {
 $user_role = get_queried_object()->roles[0];
 if( in_array( strtolower('Contributor'), $user_role ) )
     $allowed_blocks = array(
         'core/block',
         'core/image',
         'core/paragraph',
         'core/heading',
         'core/list'
     );
     return $allowed_blocks;
 }
 add_filter( 'allowed_block_types', 'restrict_blocks');

You can also filter the blocks based on post types. For example, you may think that regular users who write blog posts don’t need all the fancy block-based bells and whistles, but you might want to keep the option open for pages. In which case you can add another conditional statement like is_page() to get the result you want.

You can expand the above list to further include selected blocks by using this reference list for the default blocks in WordPress 5.0. Based on this, you can make your block selection as comprehensive as you want.

I personally feel that it’s still not as simple as adding or removing functionality in the TinyMCE editor, but it’s a start. Perhaps they’ll add additional functionality to streamline things in the future!

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Connect With Us!

Superhero Resources

Fix Common Issues

  • How To Setup Free And Automatic SSL Certificates
  • How To Setup Cloudflare With Railgun
  • How To Fix Memory Exhausted Errors In WordPress
  • How To Edit PHP Version/Upload Limit/Add Extensions
  • How To Move/Migrate Your Business To Name Hero

Free Guides

  • How To Setup NameHero Hosting
  • How To Create A Web Hosting Business With WordPress
  • How To Start A WordPress Blog
  • How To Migrate WordPress To A VPS
  • How To Speed Test And Optimize Your WordPress Website
  • Magento 2.X Installation Guide
  • How To Clean Up A WordPress Hack

Training

Recent Posts

  • Preventing Malware With WordPress Plugin Updates
  • How To Add A Subdomain In Cloudflare
  • WordPress Should Do More For Core Web Vitals
  • Is it Time to Disable Your RSS Feeds?
  • The Dangers Of FTP – Use With Extreme Care (Or Avoid)!
  • 3 Great Reasons To Use Subdomains
Subscribe in a reader
  • Web Hosting
  • WordPress Hosting
  • Reseller Hosting
  • VPS Hosting
  • Twitter
  • Facebook
  • LinkedIn
  • YouTube
  • FTC Disclosure
  • Earnings Disclosure
  • Privacy Policy

Copyright © 2021 · Smart Passive Income Pro on Genesis Framework · WordPress · Log in