固定ページのみビジュアルエディターを非表示|wordpress
wordpressで編集するときにビジュアルエディタを触らせたくないってことありませんか?
ユーザー設定でユーザー毎に設定できていたのですが、この設定がなくなりました。
すでに有効になっている場合はwordpressのバージョンアップをしても機能は生きているようです。
https://core.trac.wordpress.org/changeset/59695
function.phpに下記の設定を入れることで簡単に導入できますので、ご参考ください。
固定ページのみ適用
function disable_visual_editor_in_page($can) { if ( get_post_type() === 'page' ) { return false; } return $can; } add_filter('user_can_richedit', 'disable_visual_editor_in_page');
1. get_post_type() === ‘page’ で固定ページかどうかをチェックします
2. 固定ページの場合は false を返してビジュアルエディターを無効化します
3. それ以外の投稿タイプの場合は元の設定を維持します
注意点:
・ functions.php の編集は、子テーマで行うことをお勧めします
・ コードを追加した後、一度管理画面からログアウトして再度ログインする必要があるかもしれません
・ このコードは全ユーザーに適用されます
投稿ページも対象とする場合は下記
function disable_visual_editor_in_page($can) { if ( get_post_type() === 'page' || get_post_type() === 'post' ) { return false; } return $can; } add_filter('user_can_richedit', 'disable_visual_editor_in_page');
条件分岐にget_post_type() === ‘post’を足すだけですね。
特定のユーザーロールにのみ適用したい場合は、以下のように条件を追加できます
管理者のみ
function disable_visual_editor_in_page($can) { if ( get_post_type() === 'page' && !current_user_can('administrator') ) { return false; } return $can; } add_filter('user_can_richedit', 'disable_visual_editor_in_page');
編集者のみ
function disable_visual_editor_in_page($can) { // 現在のユーザーの情報を取得 $user = wp_get_current_user(); // 固定ページで、かつユーザーが編集者の場合のみ適用 if ( get_post_type() === 'page' && in_array('editor', $user->roles) ) { return false; } return $can; } add_filter('user_can_richedit', 'disable_visual_editor_in_page');
特定のユーザーのみ適用
ユーザーIDで指定する場合:
function disable_visual_editor_in_page($can) { // 特定のユーザーIDを配列で指定 $specific_users = array(2, 3, 4); // ここにユーザーIDを入力 if ( get_post_type() === 'page' && in_array(get_current_user_id(), $specific_users) ) { return false; } return $can; } add_filter('user_can_richedit', 'disable_visual_editor_in_page');
ユーザー名(ログイン名)で指定する場合:
function disable_visual_editor_in_page($can) { // 特定のユーザー名を配列で指定 $specific_usernames = array('username1', 'username2', 'username3'); // ここにユーザー名を入力 $current_user = wp_get_current_user(); if ( get_post_type() === 'page' && in_array($current_user->user_login, $specific_usernames) ) { return false; } return $can; } add_filter('user_can_richedit', 'disable_visual_editor_in_page');
メールアドレスで指定する場合:
function disable_visual_editor_in_page($can) { // 特定のメールアドレスを配列で指定 $specific_emails = array('user1@example.com', 'user2@example.com'); // ここにメールアドレスを入力 $current_user = wp_get_current_user(); if ( get_post_type() === 'page' && in_array($current_user->user_email, $specific_emails) ) { return false; } return $can; } add_filter('user_can_richedit', 'disable_visual_editor_in_page');
注意点:
・ ユーザーIDは管理画面のユーザー一覧で確認できます
・ コードを追加した後、一度ログアウトして再ログインする必要があります
・ 複数のユーザーを指定する場合は、配列に追加していけます
・ セキュリティのため、functions.php は子テーマで編集することをお勧めします