Hide the Visual Editor for Specific WordPress Page Templates

When a page in WordPress is driven mostly by custom metadata, the default visual editor may not be useful at all. In that case, hiding it can make the editing experience much cleaner for users.

The following code can be copied directly into the theme’s functions.php file:

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');
    }
}

After that, you can create a custom page template specifically for those pages. In that custom template, the main thing to remove is the code around the_content(), because that is what displays the visual editor’s saved content on the front end.

If you are dealing with a custom post type instead of a page template, another option is to remove support for the editor directly when the custom post type is registered, and then replace it with one or more dedicated custom fields or custom editors.

Related Posts

Leave a Reply

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