How to disable the WordPress visual editor for specific pages or posts

If you use the standard visual (WYSIWYG) WordPress editor, you probably was faced with issue when the code you entered in the text mode has been changed after switching to the visual editor mode. This is a feature of the built-in WordPress editor (TinyMCE), which is designed to bring the HTML code into compliance with markup standards. There are several ways to restrict this functionality, but you can not completely turn it off. So, how to add code that TinyMCE editor "does not like"?

To turn off the TinyMCE visual editor on certain pages or posts, you need to add the following code to the functions.php file of your active WordPress template:

function disable_visual_editor($can)
{
    global $post;

    /*
     * Disable the WordPress visual editor on the page with a specific ID (with ID = 15 in our example)
     */
    if ($post->ID == 15) {
        return false;
    }

    /*
     * Hide the TinyMCE visual editor on pages or posts with IDs 16, 25 and 30 (for cases where you want to hide the editor for several pages at once)
     */
    $disabled_IDs = array(16, 25, 30);
    if (in_array($post->ID, $disabled_IDs)) {
        return false;
    }
	
	/*
     * Disable the WordPress visual editor for all pages (i.e. for all posts with type equal "page")
     */
	$post_type = get_post_type($post);
	if ($post_type == 'page') {
        return false;
    }

    /*
     * Disable the WordPress visual editor on pages or posts with a specific page template (!!!ATTENTION!!! you need to specify the file name of page template, not just template title, for example my_page_template.php)
     */
    $page_template = get_post_meta($post->ID, '_wp_page_template', true);
    if ($page_template == 'my_page_template.php') {
        return false;
    }

    return $can;
}

add_filter('user_can_richedit', 'disable_visual_editor');

Of course, when you have access to the $post object, you can also use its other properties (post_title, post_name, etc.) to disable the visual editor for pages with a specific title, URL, etc.

Also with the use of functions such as get_the_category($post->ID); or wp_get_current_user(); you can hide the visual editor for a particular category or user (user group, specific role, etc.)

How to fully disable the ability to edit certain pages or posts content in WordPress

There are some cases when you don't need the editor at all. For example, you created a template PHP file that receives and displays data from an external source and assigned this template to one of your pages. In this case, you can completely disable the ability to edit content of this page (i.e. disable both visual and text editor), while leaving the ability to change the title, tags and so on. To completely turn off the editor, you need to add the following code to the functions.php file of your template:

function disable_content_editor()
{
    if (isset($_GET['post'])) {
        $post_ID = $_GET['post'];
    } else if (isset($_POST['post_ID'])) {
        $post_ID = $_POST['post_ID'];
    }

    if (!isset($post_ID) || empty($post_ID)) {
        return;
    }

    /*
     * Completely disable the WordPress editor for the page with a specific ID (in our example with ID = 15)
     */
    if ($post_ID == 15) {
        remove_post_type_support('page', 'editor');
    }
	
	/*
     * Fully disable the ability to edit all pages (i.e. all posts with "page" type)
     */
	$post_type = get_post_type($post_ID);
	if ($post_type == 'page') {
        return false;
    }

    /*
     * Disable editing for pages with ID 16, 25 and 30 (for cases when you want to disable the editor for several pages at once)
     */
    $disabled_IDs = array(16, 25, 30);
    if (in_array($post_ID, $disabled_IDs)) {
        remove_post_type_support('page', 'editor');
    }

    /*
     * Hide the WordPress editor on pages with a specific page template (!!!WARNING!!! you do not need to specify a template name, but the name of its file, for example my_page_template.php)
     */
    $page_template = get_post_meta($post_ID, '_wp_page_template', true);
    if ($page_template == 'my_page_template.php') {
        remove_post_type_support('page', 'editor');
    }
}

add_action('admin_init', 'disable_content_editor');