Removing the TinyMCE editor for a given page template

I was looking for a way to remove the TinyMCE Editor for a specific page template in a theme (in my case it’s page-home.php). I found the following code, which works, however, I was wondering if this could be accomplished in a better/neater way, perhaps using some of WordPress’ built-in functions for finding the ID of the page…

function hide_editor() {
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    if( !isset( $post_id ) ) return;
    $template_file = get_post_meta($post_id, '_wp_page_template', true);
    if($template_file == 'page-home.php'){ // template name here
        remove_post_type_support('page', 'editor');
    }
}
add_action( 'admin_init', 'hide_editor' );

Related posts

Leave a Reply

2 comments

  1. You can try to hook on load-page hook instead of admin_init. It is supposed to be called only when a page is being edited, and then you should be able to use the global $post variable

    function hide_editor() {
       global $post;
    
        $template_file = get_post_meta($post->ID, '_wp_page_template', true);
        if($template_file == 'page-home.php'){ // template name here
            remove_post_type_support('page', 'editor');
        }
    }
    add_action( 'load-page', 'hide_editor' );
    
  2. This worked for me:

    function hide_editor() {
      if(isset($_REQUEST['post'])){
        $post_id = $_REQUEST['post'];
        $template_file = get_post_meta($post_id, '_wp_page_template', true);
        if($template_file == 'page-home.php'){ // template name here
            remove_post_type_support('page', 'editor');
        }
      }
    }
    add_action( 'load-post.php', 'hide_editor' );