ブロックエディタのブロックツールで不要な書式を隠す

registerFormatTypeのやり方はよく紹介されているが、unregisterFormatTypeの方は見かけなかったのでメモ。

コード

functions.php

add_action( 'enqueue_block_editor_assets', function(){
  $js_uri = get_template_directory_uri() . "/js/unregister-format-type.js";
  $js_path = get_template_directory() . "/js/unregister-format-type.js";
  wp_enqueue_script("unregister-format-type-js", $js_uri, [ 'wp-rich-text', ], filemtime($js_path));
});

js/unregister-format-type.js

// unregisterFormatType 不要な書式の削除
wp.domReady(function () {
  // wp.richText.unregisterFormatType("core/bold");
  // wp.richText.unregisterFormatType("core/italic");
  // wp.richText.unregisterFormatType("core/link");
  richText.unregisterFormatType("core/code");
  richText.unregisterFormatType("core/image");
  richText.unregisterFormatType("core/keyboard");
  richText.unregisterFormatType("core/superscript");
  richText.unregisterFormatType("core/subscript");
  richText.unregisterFormatType("core/underline");
  richText.unregisterFormatType("core/text-color");
  // wp.richText.unregisterFormatType("core/strikethrough");
});

wp.domReadyに入れないとエラーになって削除されないので注意。

残すやつはコメントアウト。

参考