【Gutenberg】ブロックエディタで不要項目を削除する方法
Gutenberg
WordPress
カスタマイズ
グーテンベルグ
ブロックエディタ
不要項目
WordPressの標準エディタであるGutenberg(ブロックエディタ)。
「タイトル」や「カテゴリー」、「リビジョン」など様々な項目がありますが、サイトの作りによっては使用しない不要な項目も出てきてしまいます。
そのままにしておいても問題無いですが、個人的には使っていないのであれば削除して管理画面をスッキリさせたいなと思います。
今回の記事ではこれら使用しない不要項目を削除する方法をご紹介します。
この記事の目次を表示
個別に指定する
Gutenbergの不要項目を個別に削除する関数は以下の通りです。
投稿者
function remove_block_editor_author() {
remove_post_type_support( 'post', 'author' );
}
add_action( 'init', 'remove_block_editor_author' );
投稿フォーマット
function remove_block_editor_postformat() {
remove_post_type_support( 'post', 'post-formats' );
}
add_action( 'init', 'remove_block_editor_postformat' );
リビジョン
function remove_block_editor_revisions() {
remove_post_type_support( 'post', 'revisions' );
}
add_action( 'init', 'remove_block_editor_revisions' );
アイキャッチ
function remove_block_editor_thumbnail() {
remove_post_type_support( 'post', 'thumbnail' );
}
add_action( 'init', 'remove_block_editor_thumbnail' );
抜粋
function remove_block_editor_excerpt() {
remove_post_type_support( 'post', 'excerpt' );
}
add_action( 'init', 'remove_block_editor_excerpt' );
コメント
function remove_block_editor_comments() {
remove_post_type_support( 'post', 'comments' );
}
add_action( 'init', 'remove_block_editor_comments' );
トラックバック
function remove_block_editor_trackbacks() {
remove_post_type_support( 'post', 'trackbacks' );
}
add_action( 'init', 'remove_block_editor_trackbacks' );
カスタムフィールド
function remove_block_editor_customfields() {
remove_post_type_support( 'post', 'custom-fields' );
}
add_action( 'init', 'remove_block_editor_customfields' );
カテゴリー
function remove_block_editor_category() {
unregister_taxonomy_for_object_type( 'category', 'post' );
}
add_action( 'init', 'remove_block_editor_category' );
タグ
function remove_block_editor_tag() {
unregister_taxonomy_for_object_type( 'post_tag', 'post' );
}
add_action( 'init', 'remove_block_editor_tag' );
まとめて指定する
続いてまとめて指定する方法をご紹介します。
function remove_block_editor() {
// 投稿者
remove_post_type_support( 'post', 'author' );
// 投稿フォーマット
remove_post_type_support( 'post', 'post-formats' );
// リビジョン
remove_post_type_support( 'post', 'revisions' );
// アイキャッチ
remove_post_type_support( 'post', 'thumbnail' );
// 抜粋
remove_post_type_support( 'post', 'excerpt' );
// コメント
remove_post_type_support( 'post', 'comments' );
// トラックバック
remove_post_type_support( 'post', 'trackbacks' );
// カスタムフィールド
remove_post_type_support( 'post', 'custom-fields' );
// カテゴリー
unregister_taxonomy_for_object_type( 'category', 'post' );
// タグ
unregister_taxonomy_for_object_type( 'post_tag', 'post' );
}
add_action( 'init', 'remove_block_editor' );
管理者以外の場合のみ適応する方法
上の例では全ての権限のユーザーで指定した項目が削除されてしまいます。
最後にご紹介するのは管理者のみは全ての項目を表示し、お客さん用のアカウントとして用意する「投稿者」「編集者」等では不要項目を削除する方法です。
実装方法は以下の通りです。
function remove_block_editor() {
if( is_admin() ) {
if( ! issset( wp_get_current_user()->caps['administrator'] ) ) {
// 投稿者
remove_post_type_support( 'post', 'author' );
// 投稿フォーマット
remove_post_type_support( 'post', 'post-formats' );
// リビジョン
remove_post_type_support( 'post', 'revisions' );
// アイキャッチ
remove_post_type_support( 'post', 'thumbnail' );
// 抜粋
remove_post_type_support( 'post', 'excerpt' );
// コメント
remove_post_type_support( 'post', 'comments' );
// トラックバック
remove_post_type_support( 'post', 'trackbacks' );
// カスタムフィールド
remove_post_type_support( 'post', 'custom-fields' );
// カテゴリー
unregister_taxonomy_for_object_type( 'category', 'post' );
// タグ
unregister_taxonomy_for_object_type( 'post_tag', 'post' );
}
}
}
add_action( 'init', 'remove_block_editor' );
まとめ
いかがでしたですか?
これら使用していない不要項目を削除するだけで管理画面がスッキリ見やすくなりますね。
またお客さんが誤って操作してしまうということも防ぐことも可能です。
これらを使用し、より分かりやすい管理画面を作っていきたいですね。










