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

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

当我们添加一些元数据到 WordPress 中的某个页面的时候,有时候一些特殊模板页面(比如联系我们页面)编辑后台,你可能添加了一些表单之后,WordPress 默认的编辑器可能就用不上了,想移除默认的编辑器。为了提高用户的编辑体验,把这些页面的可视化编辑器隐藏掉是一个比较直接的办法。

直接把下面的代码复制到主题的 functions.php 中就可以了。复制之前,需要先替换下面代码中的 contact.php 为你所用主题的页面模板名称。比如,我在主题中定义了一个模板叫 page-noediter,就需要把 contact.php 替换为 page-noediter。只需要在当前主题的 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');
    }
}
温馨提示:本文最后更新于2022-05-12 19:51:01,某些文章具有时效性,若有错误或已失效,请在下方留言或联系乡野博文
您阅读这篇文章共花了: 0小时00分00秒
-----本页内容已结束,喜欢请分享!-----
© 版权声明
THE END
喜欢本站内容,请点【点赞】【分享】和【收藏】~
点赞8 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容