Possible to disable the content editor on certain page templates in wordpress backend?

I am using the advanced custom fields plugin for wordpress which means some of my pages have defunct content editors in them so I want to disable them on certain pages. I did find the code below on some websites, but this creates an error in the backend Undefined index: post and Undefined index: post_ID

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

Related posts

Leave a Reply

1 comment

  1. Use PHP function isset() to fix this erros :

    add_action( 'admin_init', 'hide_editor' );
    
    function hide_editor() {
    
            // Get the Post ID.
            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;
    
        // 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');
        }
    
    }