Changing the category for existing Gutenberg blocks

I’m trying to re-categorise some existing blocks (both core and third party). I’ve been told this should be doable using the registerBlockType filter but I haven’t had much luck.

Could someone share a working example?

TIA

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try this, it changes the core Spacer block from the Design to the Media category in the insertor:

function myprefixFilterSpacerCategory(settings, name) {
  if (name === "core/spacer") {
    // Object.assign can also be used instead of lodash.assign
    return lodash.assign({}, settings, {
      category: "media",
    });
  }
  return settings;
}

wp.hooks.addFilter(
  "blocks.registerBlockType",
  "myprefix/filter-spacer-category",
  myprefixFilterSpacerCategory
);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x