Skip to content

Instantly share code, notes, and snippets.

@knolaust
Last active April 5, 2024 12:14
Show Gist options
  • Save knolaust/b56181a52897dd5c0a2c5fa5556e0aee to your computer and use it in GitHub Desktop.
Save knolaust/b56181a52897dd5c0a2c5fa5556e0aee to your computer and use it in GitHub Desktop.
Allow/disallow certain blocks for specific post types and custom post types in WordPress' Gutenberg Editor
<?php
/**
* Allowed Block Types Configuration
* Gist Keywords: wordpress, editor, gutenberg, blocks
*
* @category WordPress
* @author Knol Aust
* @version 1.0.0
* @description Limit the blocks allowed in Gutenberg for improved content control.
*/
add_filter('allowed_block_types', 'knolaust_allowed_block_types', 10, 2);
/**
* Limit the blocks allowed in Gutenberg.
*
* @param mixed $allowed_blocks Array of allowable blocks for Gutenberg Editor.
* @param mixed $post Gets current post type.
*
* @return mixed $allowed_blocks Returns the allowed blocks.
* */
function knolaust_allowed_block_types( $allowed_blocks, $post )
{
$allowed_blocks = array(
'core/image',
'core/paragraph',
'core/heading',
'core/list'
);
/**
* 'page' is the style being filtered. To add additional types:
* if($post->post_type === 'page' || $post->post_type === 'projects') :
* where 'projects' is the custom post type or post type.
*/
if($post->post_type === 'page') :
$allowed_blocks[] = 'core/shortcode';
endif;
return $allowed_blocks;
}
/*
List of the currently available block types.
core/archives //Display a date archive of your posts.
core/audio //Embed a simple audio player.
core/avatar //Add a user’s avatar.
core/block //Create and save content to reuse across your site. Update the pattern, and the changes apply everywhere it’s used.
core/button //Prompt visitors to take action with a button-style link.
core/buttons //Prompt visitors to take action with a group of button-style links.
core/calendar //A calendar of your site’s posts.
core/categories //Display a list of all categories.
core/code //Display code snippets that respect your spacing and tabs.
core/column //A single column within a columns block.
core/columns //Display content in multiple columns, with blocks added to each column.
core/comment-author-name //Displays the name of the author of the comment.
core/comment-content //Displays the contents of a comment.
core/comment-date //Displays the date on which the comment was posted.
core/comment-edit-link //Displays a link to edit the comment in the WordPress Dashboard. This link is only visible to users with the edit comment capability.
core/comment-reply-link //Displays a link to reply to a comment.
core/comment-template //Contains the block elements used to display a comment, like the title, date, author, avatar and more.
core/comments //An advanced block that allows displaying post comments using different visual configurations.
core/comments-pagination //Displays a paginated navigation to next/previous set of comments, when applicable.
core/comments-pagination-next //Displays the next comment's page link.
core/comments-pagination-numbers //Displays a list of page numbers for comments pagination.
core/comments-pagination-previous //Displays the previous comment's page link.
core/comments-title //Displays a title with the number of comments
core/cover //Add an image or video with a text overlay.
core/details //Hide and show additional content.
core/embed //Add a block that displays content pulled from other sites, like Twitter or YouTube.
core/file //Add a link to a downloadable file.
core/footnotes //—
core/freeform //Use the classic WordPress editor.
core/gallery //Display multiple images in a rich gallery.
core/group //Gather blocks in a layout container.
core/heading //Introduce new sections and organize content to help visitors (and search engines) understand the structure of your content.
core/home-link //Create a link that always points to the homepage of the site. Usually not necessary if there is already a site title link present in the header.
core/html //Add custom HTML code and preview it as you edit.
core/image //Insert an image to make a visual statement.
core/latest-comments //Display a list of your most recent comments.
core/latest-posts //Display a list of your most recent posts.
core/legacy-widget //Display a legacy widget.
core/list //Create a bulleted or numbered list.
core/list-item //Create a list item.
core/loginout //Show login & logout links.
core/media-text //Set media and words side-by-side for a richer layout.
core/missing //Your site doesn’t include support for this block.
core/more //Content before this block will be shown in the excerpt on your archives page.
core/navigation //A collection of blocks that allow visitors to get around your site.
core/navigation-link //Add a page, link, or another item to your navigation.
core/navigation-submenu //Add a submenu to your navigation.
core/nextpage //Separate your content into a multi-page experience.
core/page-list //Display a list of all pages.
core/page-list-item //Displays a page inside a list of all pages.
core/paragraph //Start with the basic building block of all narrative.
core/pattern //Show a block pattern.
core/post-author //Display post author details such as name, avatar, and bio.
core/post-author-biography //The author biography.
core/post-author-name //The author name.
core/post-comments //—
core/post-comments-form //Display a post's comments form.
core/post-content //Displays the contents of a post or page.
core/post-date //Display the publish date for an entry such as a post or page.
core/post-excerpt //Display the excerpt.
core/post-featured-image //Display a post's featured image.
core/post-navigation-link //Displays the next or previous post link that is adjacent to the current post.
core/post-template //Contains the block elements used to render a post, like the title, date, featured image, content or excerpt, and more.
core/post-terms //Post terms.
core/post-title //Displays the title of a post, page, or any other content-type.
core/preformatted //Add text that respects your spacing and tabs, and also allows styling.
core/pullquote //Give special visual emphasis to a quote from your text.
core/query //An advanced block that allows displaying post types based on different query parameters and visual configurations.
core/query-no-results //Contains the block elements used to render content when no query results are found.
core/query-pagination //Displays a paginated navigation to next/previous set of posts, when applicable.
core/query-pagination-next //Displays the next posts page link.
core/query-pagination-numbers //Displays a list of page numbers for pagination
core/query-pagination-previous //Displays the previous posts page link.
core/query-title //Display the query title.
core/quote //Give quoted text visual emphasis. "In quoting others, we cite ourselves." — Julio Cortázar
core/read-more //Displays the link of a post, page, or any other content-type.
core/rss //Display entries from any RSS or Atom feed.
core/search //Help visitors find your content.
core/separator //Create a break between ideas or sections with a horizontal separator.
core/shortcode //Insert additional custom elements with a WordPress shortcode.
core/site-logo //Display an image to represent this site. Update this block and the changes apply everywhere.
core/site-tagline //Describe in a few words what the site is about. The tagline can be used in search results or when sharing on social networks even if it’s not displayed in the theme design.
core/site-title //Displays the name of this site. Update the block, and the changes apply everywhere it’s used. This will also appear in the browser title bar and in search results.
core/social-link //Display an icon linking to a social media profile or site.
core/social-links //Display icons linking to your social media profiles or sites.
core/spacer //Add white space between blocks and customize its height.
core/table //Create structured content in rows and columns to display information.
core/tag-cloud //A cloud of your most used tags.
core/template-part //Edit the different global regions of your site, like the header, footer, sidebar, or create your own.
core/term-description //Display the description of categories, tags and custom taxonomies when viewing an archive.
core/text-columns //This block is deprecated. Please use the Columns block instead.
core/verse //Insert poetry. Use special spacing formats. Or quote song lyrics.
core/video //Embed a video from your media library or upload a new one.
core/widget-group //—
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment