Wordpress Snippets

Disable Content Editor for Specific Page Template

While working on a project I needed a way to disable content editor on a specific page. Why you asked? On this project, I created a custom page which has lots of meta-boxes on editor screen and we were not using visual editor for adding any content on that page so we wanted to disable content editor to avoid confusion for end user. As a solution, the first thing that came to my mind is to dynamically hide visual editor with JavaScript and CSS. Although it would work just find but it’s not a proper solution so I tried to follow a different approach to accomplish this task.

Finally, I came across the WordPress function remove_post_type_support which is used to remove support of certain features for a given post type. So I tried to utilize this function to disable content editor on edit screen for a specific page template. I put together a code snippet that you can add in functions.php of your WordPress theme. It will disable visual content editor when a page is using a specific template.

// disable content editor for page template
function wpcs_disable_content_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-custom.php' ) {
        remove_post_type_support( 'page', 'editor' );
    }

}
add_action( 'admin_init', 'wpcs_disable_content_editor' );

Don’t forget to change the name of the template file on above code. I have used page-custom.php in this example but it can be changed to anything.

Hire Us. Or just say Hi!
Need a job? Apply to get one.