WordPressのブロックエディタ(Gutenberg)では、さまざまなブロックが用意されていますが、全てのブロックを使わせるとデザインが崩れたり、管理が煩雑になったりします。
この記事では、使用できるブロックを個別に制御する方法を紹介します。不要なブロックを無効にして、必要なブロックだけを有効化しましょう。
使用ブロックを制御する基本コード
以下のコードを、テーマの functions.php
ファイルに追加します。
function my_allowed_block_types($allowed_blocks, $editor_context) {
// 投稿タイプごとに制御したい場合の例(必要に応じて調整)
if (!empty($editor_context->post)) {
$post_type = $editor_context->post->post_type;
if ($post_type === 'post' || $post_type === 'page') {
return [
'core/paragraph', // 段落
'core/heading', // 見出し
'core/image', // 画像
'core/list', // リスト
'core/table', // テーブル
'core/buttons', // ボタン
'core/quote', // 引用
'core/separator', // 区切り
'core/group', // グループ
'core/columns', // カラム
// 必要なブロックをここに追加
];
}
}
// その他の投稿タイプはすべて許可
return $allowed_blocks;
}
add_filter('allowed_block_types_all', 'my_allowed_block_types', 10, 2);
コアブロックの識別子一覧(完全版)
下記がWordPressで使用される主要なブロックの識別子一覧です。必要なものをコードにコピペして使ってください。
■ テキスト系
core/paragraph
(段落)core/heading
(見出し)core/list
(リスト)core/quote
(引用)core/code
(コード)core/freeform
(クラシック)core/preformatted
(整形済み)core/pullquote
(プルクオート)core/verse
(詩)
■ メディア系
core/image
(画像)core/gallery
(ギャラリー)core/audio
(音声)core/cover
(カバー)core/file
(ファイル)core/media-text
(メディアと文章)core/video
(動画)
■ デザイン系
core/button
(ボタン)core/buttons
(ボタン(複数))core/columns
(カラム)core/column
(カラムの中身)core/group
(グループ)core/separator
(区切り線)core/spacer
(スペーサー)core/details
(詳細ブロック)
■ ウィジェット系
core/shortcode
(ショートコード)core/archives
(アーカイブ)core/calendar
(カレンダー)core/categories
(カテゴリー)core/latest-comments
(最新のコメント)core/latest-posts
(最新の投稿)core/rss
(RSS)core/search
(検索)core/tag-cloud
(タグクラウド)
■ テーマ関連ブロック(FSE用)
core/site-logo
core/site-title
core/site-tagline
core/post-title
core/post-content
core/post-date
core/post-featured-image
core/post-terms
core/query
core/post-template
core/query-pagination
core/query-pagination-next
core/query-pagination-previous
core/comments
core/comments-title
core/comment-content
core/comment-date
core/comment-author-name
core/comment-edit-link
core/comment-reply-link
■ 埋め込み系(Embed)
core/embed-youtube
core/embed-twitter
core/embed-facebook
core/embed-instagram
core/embed-vimeo
core/embed-soundcloud
core/embed-spotify
core/embed-flickr
core/embed-wordpress
core/embed
(汎用埋め込み)
補足:すべてのブロックを調べるには?
管理画面で以下のコードをブラウザの開発者ツール(Console)に入力すると、現在登録されているブロック一覧が取得できます。
wp.blocks.getBlockTypes().map(b => b.name)
おわりに
ブロックエディタは便利ですが、自由すぎると逆に使いづらくなることも。ぜひ今回のコードを活用して、必要なブロックだけが使える編集環境を整えてみてください!