WordPres5.8対応 ブロックを制御する方法

【WP5.8対応版】ブロック一覧に余分なブロックを表示しない方法と、YouTubeなど一部の埋め込みブロックを残す方法

WP5.8から、allowed_block_types が非推奨となりました。 allowed_block_types_all を使います。
埋め込みブロックで、Twitter/YouTubeなどを残して、その他の埋め込みブロックを削除する方法も併せて紹介。

使うブロックのみを登録する、ホワイトリスト方式

使いたいブロックを登録します。
使わないブロックは、行ごと削除するかコメントアウトしてお使いください。
ここで過去の投稿で利用しているブロックを登録から外しても、投稿済みのブロックの編集は可能です。

function allowed_block_types_all( $allowed_block_types ) {
	$allowed_block_types = array(
		// テキスト
		'core/paragraph',              // 段落
		'core/heading',                // 見出し
		'core/list',                   // リスト
		'core/quote',                  // 引用
		'core/code',                   // コード
		'core/freeform',               // クラシック
		'core/preformatted',           // 整形済みテキスト
		'core/pullquote',              // プルクオート
		'core/table',                  // テーブル
		'core/verse',                  // 詩

		// メディア
		'core/image',                  // 画像
		'core/gallery',                // ギャラリー
		'core/audio',                  // 音声
		'core/cover',                  // カバー
		'core/file',                   // ファイル
		'core/media-text',             // メディアとテキスト
		'core/video',                  // 動画

		// デザイン
		'core/buttons',                // ボタン
		'core/columns',                // カラム
		'core/group',                  // グループ
		'core/more',                   // 続きを読む
		'core/nextpage',               // ページ区切り
		'core/separator',              // 区切り
		'core/spacer',                 // スペーサー
		'core/site-logo',              // サイトロゴ
		'core/site-tagline',           // サイトのキャッチフレーズ
		'core/site-title',             // サイトのタイトル
		'core/query-title',            // アーカイブタイトル
		'core/post-terms',             // 投稿カテゴリー & 投稿タグ

		// ウィジェット
		'core/shortcode',              // ショートコード
		'core/archives',               // アーカイブ
		'core/calendar',               // カレンダー
		'core/categories',             // カテゴリー
		'core/html',                   // カスタムHTML
		'core/latest-comments',        // 最新のコメント
		'core/latest-posts',           // 最新の投稿
		'core/page-list',              // 固定ページリスト
		'core/rss',                    // RSS
		'core/social-links',           // ソーシャルアイコン
		'core/tag-cloud',              // タグクラウド
		'core/search',                 // 検索

		// テーマ
		'core/query',                  // クエリーループ & 投稿一覧
		'core/post-title',             // 投稿タイトル
		'core/post-content',           // 投稿コンテンツ
		'core/post-date',              // 投稿日
		'core/post-excerpt',           // 投稿の抜粋
		'core/post-featured-image',    // 投稿のアイキャッチ
		'core/loginout',               // ログイン / ログアウト

		// 埋め込み
		'core/embed',                  // Embed

		// 再利用ブロック
		'core/block',                  // 再利用ブロック
	);
	return $allowed_block_types;
}
add_filter( 'allowed_block_types', 'allowed_block_types_all' );

使わないブロックを登録する、ブラックリスト方式

使わないブロックを登録します。
使うブロックは、行ごと削除するかコメントアウトしてお使いください。
ここで過去の投稿で利用しているブロックを登録すると、再編集の際に投稿済みのブロックでエラーとなります。
remove-block.jsとしてテーマフォルダに設置します。

wp.domReady( () => {
	// テキスト
	wp.blocks.unregisterBlockType( 'core/paragraph' );                   // 段落
	wp.blocks.unregisterBlockType( 'core/heading' );                     // 見出し
	wp.blocks.unregisterBlockType( 'core/list' );                        // リスト
	wp.blocks.unregisterBlockType( 'core/quote' );                       // 引用
	wp.blocks.unregisterBlockType( 'core/code' );                        // コード
	wp.blocks.unregisterBlockType( 'core/freeform' );                    // クラシック
	wp.blocks.unregisterBlockType( 'core/preformatted' );                // 整形済みテキスト
	wp.blocks.unregisterBlockType( 'core/pullquote' );                   // プルクオート
	wp.blocks.unregisterBlockType( 'core/table' );                       // テーブル
	wp.blocks.unregisterBlockType( 'core/verse' );                       // 詩

	// メディア
	wp.blocks.unregisterBlockType( 'core/image' );                       // 画像
	wp.blocks.unregisterBlockType( 'core/gallery' );                     // ギャラリー
	wp.blocks.unregisterBlockType( 'core/audio' );                       // 音声
	wp.blocks.unregisterBlockType( 'core/cover' );                       // カバー
	wp.blocks.unregisterBlockType( 'core/file' );                        // ファイル
	wp.blocks.unregisterBlockType( 'core/media-text' );                  // メディアとテキスト
	wp.blocks.unregisterBlockType( 'core/video' );                       // 動画

	// デザイン
	wp.blocks.unregisterBlockType( 'core/buttons' );                     // ボタン
	wp.blocks.unregisterBlockType( 'core/columns' );                     // カラム
	wp.blocks.unregisterBlockType( 'core/group' );                       // グループ
	wp.blocks.unregisterBlockType( 'core/more' );                        // 続きを読む
	wp.blocks.unregisterBlockType( 'core/nextpage' );                    // ページ区切り
	wp.blocks.unregisterBlockType( 'core/separator' );                   // 区切り
	wp.blocks.unregisterBlockType( 'core/spacer' );                      // スペーサー
	wp.blocks.unregisterBlockType( 'core/site-logo' );                   // サイトロゴ
	wp.blocks.unregisterBlockType( 'core/site-tagline' );                // サイトのキャッチフレーズ
	wp.blocks.unregisterBlockType( 'core/site-title' );                  // サイトのタイトル
	wp.blocks.unregisterBlockType( 'core/query-title' );                 // アーカイブタイトル
	wp.blocks.unregisterBlockType( 'core/post-terms' );                  // 投稿カテゴリー & 投稿タグ

	// ウィジェット
	wp.blocks.unregisterBlockType( 'core/shortcode' );                   // ショートコード
	wp.blocks.unregisterBlockType( 'core/archives' );                    // アーカイブ
	wp.blocks.unregisterBlockType( 'core/calendar' );                    // カレンダー
	wp.blocks.unregisterBlockType( 'core/categories' );                  // カテゴリー
	wp.blocks.unregisterBlockType( 'core/html' );                        // カスタムHTML
	wp.blocks.unregisterBlockType( 'core/latest-comments' );             // 最新のコメント
	wp.blocks.unregisterBlockType( 'core/latest-posts' );                // 最新の投稿
	wp.blocks.unregisterBlockType( 'core/page-list' );                   // 固定ページリスト
	wp.blocks.unregisterBlockType( 'core/rss' );                         // RSS
	wp.blocks.unregisterBlockType( 'core/social-links' );                // ソーシャルアイコン
	wp.blocks.unregisterBlockType( 'core/tag-cloud' );                   // タグクラウド
	wp.blocks.unregisterBlockType( 'core/search' );                      // 検索

	// テーマ
	wp.blocks.unregisterBlockType( 'core/query' );                       // クエリーループ & 投稿一覧
	wp.blocks.unregisterBlockType( 'core/post-title' );                  // 投稿タイトル
	wp.blocks.unregisterBlockType( 'core/post-content' );                // 投稿コンテンツ
	wp.blocks.unregisterBlockType( 'core/post-date' );                   // 投稿日
	wp.blocks.unregisterBlockType( 'core/post-excerpt' );                // 投稿の抜粋
	wp.blocks.unregisterBlockType( 'core/post-featured-image' );         // 投稿のアイキャッチ
	wp.blocks.unregisterBlockType( 'core/loginout' );                    // ログイン / ログアウト

	// 埋め込み
	wp.blocks.unregisterBlockVariation( 'core/embed', 'twitter' );       // Twitter
	wp.blocks.unregisterBlockVariation( 'core/embed', 'youtube' );       // YouTube
	wp.blocks.unregisterBlockVariation( 'core/embed', 'wordpress' );     // WordPress
	wp.blocks.unregisterBlockVariation( 'core/embed', 'soundcloud' );    // SoundCloud
	wp.blocks.unregisterBlockVariation( 'core/embed', 'spotify' );       // Spotify
	wp.blocks.unregisterBlockVariation( 'core/embed', 'flickr' );        // Flickr
	wp.blocks.unregisterBlockVariation( 'core/embed', 'vimeo' );         // Vimeo
	wp.blocks.unregisterBlockVariation( 'core/embed', 'animoto' );       // Animoto
	wp.blocks.unregisterBlockVariation( 'core/embed', 'cloudup' );       // Cloudup
	wp.blocks.unregisterBlockVariation( 'core/embed', 'crowdsignal' );   // Crowdsignal
	wp.blocks.unregisterBlockVariation( 'core/embed', 'dailymotion' );   // Dailymotion
	wp.blocks.unregisterBlockVariation( 'core/embed', 'imgur' );         // Imgur
	wp.blocks.unregisterBlockVariation( 'core/embed', 'issuu' );         // Issuu
	wp.blocks.unregisterBlockVariation( 'core/embed', 'kickstarter' );   // Kickstarter
	wp.blocks.unregisterBlockVariation( 'core/embed', 'meetup-com' );    // Meetup.com
	wp.blocks.unregisterBlockVariation( 'core/embed', 'mixcloud' );      // Mixcloud
	wp.blocks.unregisterBlockVariation( 'core/embed', 'reddit' );        // Reddit
	wp.blocks.unregisterBlockVariation( 'core/embed', 'reverbnation' );  // ReverbNation
	wp.blocks.unregisterBlockVariation( 'core/embed', 'screencast' );    // Screencast
	wp.blocks.unregisterBlockVariation( 'core/embed', 'scribd' );        // Scribd
	wp.blocks.unregisterBlockVariation( 'core/embed', 'slideshare' );    // Slideshare
	wp.blocks.unregisterBlockVariation( 'core/embed', 'smugmug' );       // SmugMug
	wp.blocks.unregisterBlockVariation( 'core/embed', 'speaker-deck' );  // Speaker Deck
	wp.blocks.unregisterBlockVariation( 'core/embed', 'tiktok' );        // TikTok
	wp.blocks.unregisterBlockVariation( 'core/embed', 'ted' );           // TED
	wp.blocks.unregisterBlockVariation( 'core/embed', 'tumblr' );        // Tumblr
	wp.blocks.unregisterBlockVariation( 'core/embed', 'videopress' );    // VideoPress
	wp.blocks.unregisterBlockVariation( 'core/embed', 'wordpress-tv' );  // WordPress.tv
	wp.blocks.unregisterBlockVariation( 'core/embed', 'amazon-kindle' ); // Amazon Kindle
} );

次に、functions.phpremove-block.js を読み込ませます。
最後の引数をtrue として、</body>の前で読み込ませます。

add_action( 'enqueue_block_editor_assets', function() {
	wp_enqueue_script( 'remove-block', get_template_directory_uri().'/js/remove-block.js', array(), false, true );
} );

一部のサービスの埋め込みブロックを消す方法

まずはホワイトリスト方式で使うブロックを登録します。
この時に、embedを必ず登録します。
次に、ブラックリスト方式の remove-block.js の62行目以降の不要なサービスを登録します。

functions.phpやremove-block.jsなどを使う理由は、過去にどんなブロックで投稿がされてるか不明なため、基本的にホワイトリスト方式を使い、埋め込みサービスの部分のみブラックリスト方式を採用しています。
また基本的にコメントアウトで使う方が、クライアントによって微妙に要件が違う場合に時短対応できて便利!