1

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' );
1
  • I haven't tested the code but that looks fine to me.
    – s_ha_dum
    Feb 10, 2013 at 20:12

2 Answers 2

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
  • I finally got the time to test out your code. It doesn't seem to work unfortunately...
    – Ian Muscat
    Feb 22, 2013 at 21:18
  • It doesn't work. In order to make it work you have to use the hook admin_init. Unfortunally doing this the text editor also disappears :/ Apr 5, 2022 at 8:11
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.