隐藏WordPress指定页面模板的可视化编辑器

当我们添加一些元数据到 WordPress 中的某个页面的时候,WordPress 默认的编辑器可能就用不上了。为了提高用户的编辑体验,把这些页面的可视化编辑器隐藏掉是一个比较直接的办法。

直接把下面的代码复制到主题的 functions.php 中就可以了。

add_action( 'admin_init', 'hide_editor' );

function hide_editor() {
	// Get the Post ID.
	$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
	if( !isset( $post_id ) ) return;

	// Get the name of the Page Template file.
	$template_file = get_post_meta($post_id, '_wp_page_template', true);
    
    if($template_file == 'contact.php'){ // edit the template name
    	remove_post_type_support('page', 'editor');
    }
}

复制之后,我们可以根据需要创建一个自定义页面模板专门为这一类页面显示内容,在自定义页面模板中,我们只需要找到 the_content() 代码,这个代码的作用就是显示页面可视化编辑器的内容,我们只需要他相关的代码即可。

如果我们需要处理的是一个自定义文章类型,我们可以在创建自定义文章类型的时候,通过指定文章类型的 supports 参数,直接隐藏掉文章正文的可视化编辑器,然后根据需要添加一个或多个用于自定义字段的可视化编辑器。

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *