megclaypool
9/25/2019 - 9:13 PM

Blacklisting Specific Gutenberg Blocks

File structure:

|-- radicati
    |-- dist
    |-- lib
        |-- acf_gutenberg_blocks
        |-- filters
            |-- gutenberg_blacklist.js
            |-- gutenberg_blacklist.php
            |-- update_button_href.php
...

require_once("lib/filters/gutenberg_blacklist.php");

...
<?php

/**
* Remove certain blocks.
*/
function radicati_blacklist_blocks() {
  // echo ('Yup, the gutenberg blacklist php code is being called!');

  wp_enqueue_script('gutenbergBlacklist', get_stylesheet_directory_uri() . "/lib/filters/gutenberg_blacklist.js", ['wp-blocks'], 1.0, false);
}

add_action('enqueue_block_editor_assets', 'radicati_blacklist_blocks');
window.onload = function () {
  let blacklist = [
    "core/avatar",
    "core/comment-author-avatar",
    "core/comment-author-name",
    "core/comment-content",
    "core/comment-date",
    "core/comment-edit-link",
    "core/comment-reply-link",
    "core/comments-query-loop",
    "core/comments-pagination",
    "core/comments-pagination-next",
    "core/comments-pagination-numbers",
    "core/comments-pagination-previous",
    "core/comments-title",
    "core/loginout",
    "core/navigation",
    "core/pattern",
    "core/post-author",
    "core/post-author-biography",
    "core/post-author-name",
    "core/post-comment",
    "core/post-comments",
    "core/post-comments-count",
    "core/post-comments-form",
    "core/post-comments-link",
    "core/post-content",
    "core/post-date",
    "core/post-excerpt",
    "core/post-featured-image",
    "core/post-navigation-link",
    "core/post-template",
    "core/post-terms",
    "core/post-title",
    "core/query",
    "core/query-no-results",
    "core/query-pagination",
    "core/query-pagination-next",
    "core/query-pagination-numbers",
    "core/query-pagination-previous",
    "core/query-title",
    "core/read-more",
    "core/site-logo",
    "core/site-tagline",
    "core/site-title",
    "core/template-part",
    "core/term-description",
  ];

  let activeBlocks = wp.blocks.getBlockTypes();
  let disabledBlocks = [];

  activeBlocks.forEach((element) => {
    if (blacklist.includes(element.name)) {
      wp.blocks.unregisterBlockType(element.name);
      disabledBlocks.push(element.name);
    }
  });

  if (disabledBlocks.length > 0) {
    console.log(
      "Please note: the following Gutenberg blocks are being disabled in gutenberg_blacklist.js\n---"
    );
    disabledBlocks.forEach((element) => {
      console.log(element);
    });

    console.log("---");
  }

  // Common blocks
  // core/cover
  // core/gallery

  // // Formatting
  // core/verse
  // core/button
  // core/pullquote
  // core/preformatted
  // core/code
  // core/freeform

  // // Layout
  // core/columns
  // core/more
  // core/nextpage
  // core/spacer

  // // Widgets
  // core/archives
  // core/categories
  // core/latest-comments
  // core/latest-posts
  // core/calendar
  // core/rss
  // core/search

  // // Yoast
  // yoast/faq-block
  // yoast/how-to-block

  // Theme Blocks
  // core/avatar
  // core/comment-author-avatar
  // core/comment-author-name
  // core/comment-content
  // core/comment-date
  // core/comment-edit-link
  // core/comment-reply-link
  // core/comments-query-loop
  // core/comments-pagination
  // core/comments-pagination-next
  // core/comments-pagination-numbers
  // core/comments-pagination-previous
  // core/comments-title
  // core/loginout;
  // core/navigation;
  // core/pattern;
  // core/post-author
  // core/post-author-biography
  // core/post-author-name
  // core/post-comment
  // core/post-comments
  // core/post-comments-count
  // core/post-comments-form
  // core/post-comments-link
  // core/post-content
  // core/post-date
  // core/post-excerpt
  // core/post-featured-image
  // core/post-navigation-link
  // core/post-template
  // core/post-terms
  // core/post-title
  // core / query;
  // core/query-no-results
  // core/query-pagination
  // core/query-pagination-next
  // core/query-pagination-numbers
  // core/query-pagination-previous
  // core/query-title
  // core/read-more
  // core/site-logo
  // core/site-tagline
  // core/site-title
  // core/template-part
  // core/term-description
};
// Note: I don't know why, but for some reason, I needed window.onload rather than the wp.domReady() recommended in the official documentation

window.onload = function() {
  // Common blocks
  wp.blocks.unregisterBlockType("core/cover");
  wp.blocks.unregisterBlockType("core/gallery");
  // Formatting
  wp.blocks.unregisterBlockType("core/verse");
  wp.blocks.unregisterBlockType("core/pullquote");
  wp.blocks.unregisterBlockType("core/preformatted");
  wp.blocks.unregisterBlockType("core/code");
  wp.blocks.unregisterBlockType("core/freeform");
  // Layout
  wp.blocks.unregisterBlockType("core/columns");
  wp.blocks.unregisterBlockType("core/more");
  wp.blocks.unregisterBlockType("core/nextpage");
  wp.blocks.unregisterBlockType("core/spacer");
  // Widgets
  wp.blocks.unregisterBlockType("core/archives");
  wp.blocks.unregisterBlockType("core/categories");
  wp.blocks.unregisterBlockType("core/latest-comments");
  wp.blocks.unregisterBlockType("core/latest-posts");
  wp.blocks.unregisterBlockType("core/calendar");
  wp.blocks.unregisterBlockType("core/rss");
  wp.blocks.unregisterBlockType("core/search");
  // Yoast
  wp.blocks.unregisterBlockType("yoast/faq-block");
  wp.blocks.unregisterBlockType("yoast/how-to-block");

  console.log("Note: Some Gutenberg blocks are being disabled in gutenberg_blacklist.js");
};