0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WPの特定の固定ページのエディタを非表示にする

Posted at

TL;DR

hoge,fugaってスラッグの固定ページでエディタを非表示にしたければ以下のようにします

add_filter('use_block_editor_for_post',function($use_block_editor,$post){
	if($post->post_type==='page'){
		if(in_array($post->post_name,['hoge','fuga'])){
			remove_post_type_support('page','editor');
			return false;
		}
	}
	return $use_block_editor;
},10,2);

解説

WPの記事編集画面はwp-admin/post.php?action=editをつけて呼ばれます
ブロックエディタが有効な投稿であればwp-admin/edit-form-blocks.phpが読み込まれますが
このwp-admin/edit-form-blocks.php内ではエディタ部分だけを無効化する手段がないため
まずuse_block_editor_for_post()falseを返すようにしてブロックエディタを無効にします

use_block_editor_for_post()の戻り値は
use_block_editor_for_postフィルタフックを通されるのでこれを利用します

use_block_editor_for_post

add_filter('use_block_editor_for_post',function($use_block_editor,$post){
	if($post->post_type==='page'){
		if(in_array($post->post_name,['hoge','fuga'])){
			return false;
		}
	}
	return $use_block_editor;
},10,2);

これでhoge,fugaでブロックエディタを無効化できました

するとwp-admin/edit-form-blocks.phpの代わりに
wp-admin/edit-form-advanced.phpが読み込まれるようになり
旧エディタで記事編集画面が開くようになります

旧エディタであればエディタ部分だけを無効化することができます
先のフィルタ処理のついでにremove_post_type_support()してエディタを無効化しましょう

add_filter('use_block_editor_for_post',function($use_block_editor,$post){
	if($post->post_type==='page'){
		if(in_array($post->post_name,['hoge','fuga'])){
			remove_post_type_support('page','editor');
			return false;
		}
	}
	return $use_block_editor;
},10,2);

以上です

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?