特定の標準ブロックを使えないようにする方法

WordPressには様々なブロックが標準で用意されています。

  • 段落
  • 引用
  • 画像
  • カラム
  • ボタン

など、標準では数多くのブロックが用意されていて、それらだけでも素敵なコンテンツを形成することができます。

しかし、受託案件などにおいては、利用者を迷わせない為にも利用を想定しないブロックはあらかじめ選択できないようにするという対応を求めれる場合があります。

その際の方法をメモしておきます。

任意のデフォルトブロックを削除(非表示)する

コードはこのようになります。

add_filter( 'allowed_block_types', 'olein_allowed_block_types' );
function olein_allowed_block_types( $allowed_blocks ) {

	return array(
		'core/image',
		'core/paragraph',
		'core/heading',
		'core/list'
	);

}

配列部分に利用させないブロックのスラッグを入れておきます。

ブロックのスラッグについて

一般ブロック

  • core/paragraph
  • core/image
  • core/heading
  • core/gallery
  • core/list
  • core/quote
  • core/audio
  • core/cover
  • core/file
  • core/video

フォーマット

  • core/table
  • core/verse
  • core/code
  • core/freeform — Classic
  • core/html — Custom HTML
  • core/preformatted
  • core/pullquote

レイアウト

  • core/buttons
  • core/text-columns 
  • core/media-text
  • core/more
  • core/nextpage 
  • core/separator
  • core/spacer

ウィジェット

  • core/shortcode
  • core/archives
  • core/categories
  • core/latest-comments
  • core/latest-posts
  • core/calendar
  • core/rss
  • core/search
  • core/tag-cloud

埋め込み

  • core/embed
  • core-embed/twitter
  • core-embed/youtube
  • core-embed/facebook
  • core-embed/instagram
  • core-embed/wordpress
  • core-embed/soundcloud
  • core-embed/spotify
  • core-embed/flickr
  • core-embed/vimeo
  • core-embed/animoto
  • core-embed/cloudup
  • core-embed/collegehumor
  • core-embed/dailymotion
  • core-embed/funnyordie
  • core-embed/hulu
  • core-embed/imgur
  • core-embed/issuu
  • core-embed/kickstarter
  • core-embed/meetup-com
  • core-embed/mixcloud
  • core-embed/photobucket
  • core-embed/polldaddy
  • core-embed/reddit
  • core-embed/reverbnation
  • core-embed/screencast
  • core-embed/scribd
  • core-embed/slideshare
  • core-embed/smugmug
  • core-embed/speaker
  • core-embed/ted
  • core-embed/tumblr
  • core-embed/videopress
  • core-embed/wordpress-tv

投稿タイプごとに任意ブロックを削除する

add_filter( ‘allowed_block_types’, ‘olein_allowed_block_types’, 10, 2 );
function olein_allowed_block_types( $allowed_blocks, $post ) {

	$allowed_blocks = array(
		‘core/image’,
		‘core/paragraph’,
		‘core/heading’,
		‘core/list’
	);

	if( $post->post_type === ‘page’ ) {
		$allowed_blocks[] = ‘core/shortcode’;
	}

	return $allowed_blocks;

}

このような形で色々と使いまわせると思います。権限別に表示させるブロックをコントロールすることも可能です。

書籍を出版しました!

WordPress デフォルトテーマ Twenty Twenty-Four を使って、シンプルなブログやポートフォリオサイト、そしてコーポレートサイトを作りながら、ブロックテーマやサイトエディターの基本を理解することができます。