固定ページのみビジュアルエディタの使用を不可にする

6968 Views
WordPress
固定ページのみビジュアルエディタの使用を不可にする

WordPressでサイトを作成している際、固定ページはよく使うと思いますがその中で問題になりやすいのが「ビジュアル」「テキスト」のモード切り替えだと思います。

切り替えるたびに自動でpタグなどがついてしまいコーディングの仕方によってはサイトが崩れることも。。。

この問題を解決させるには固定ページでそもそも「ビジュアルモード」が存在しなければいいのだ!

ということで固定ページでビジュアルモードを使用不可にする方法をご紹介

SNSでシェア♪

スポンサーリンク

 

目次

固定ページでビジュアルモードを使用不可にする方法

現在使用中のfunctions.phpに以下のコードを追加するだけです!

<?php
//固定ページでビジュアルエディタを利用できないようにする
function disable_visual_editor_in_page() {
	global $typenow;
	if ($typenow == 'page') add_filter('user_can_richedit', 'disable_visual_editor_filter');
}
function disable_visual_editor_filter() {
	return false;
}
add_action('load-post.php', 'disable_visual_editor_in_page');
add_action('load-post-new.php', 'disable_visual_editor_in_page');
?>

以上。

これでHTMLが崩れてしまう問題が解決できます。

 

ちなみに。。。

特定のカスタム投稿でもビジュアルモードを無効にしたい場合は

<?php
//固定ページやカスタム投稿でビジュアルエディタを利用できないようにする
function disable_visual_editor_in_page() {
	global $typenow;
	$disabledPostType = array("page", "post_type");
	if (in_array($typenow, $disabledPostType)) add_filter('user_can_richedit', 'disable_visual_editor_filter');
}
function disable_visual_editor_filter() {
	return false;
}
add_action('load-post.php', 'disable_visual_editor_in_page');
add_action('load-post-new.php', 'disable_visual_editor_in_page');
?>

5行目の箇所にポストタイプを追加してあげるだけで対応可能です!

 

以上!

SNSでシェア♪

スポンサーリンク

関連記事