Hide page visual editor if template is selected – redux

The answer given to a previous and identical question is unfortunately not working on my WP install.

I’m using a version of the function recommended there.

function nim_hide_editor() {
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    $template = get_post_meta( $post_ID , '_wp_page_template', true );

    if($template == 'portfolio.php'){
        /*echo "<style>#postdivrich{display:none;}</style>";*/
        remove_post_type_support( 'page', 'editor' );
    }
}
add_action('admin_init', 'nim_hide_editor');

I’ve included the commented-out line to show that I tried the technique of echoing the style tag mentioned in the alternate answer, and neither of them worked when selecting by page template. However…

Two things seem incongruous:

  • I’ve looked in my SQL database via CPanel and “portfolio.php” is indeed the value stored in post_meta for the pages i’m trying to use this on.

  • Selecting the page by $post_ID, and using remove_post_type_support, works.

        if($post_ID == '13'){
        remove_post_type_support( 'page', 'editor' );
    }
    

I’m going a bit crazy – it seems like I’ve triple-checked everything. Obviously I could hide that editor with an array of post_ID’s that use that template, but that won’t work for future pages that use that template.

1 Answer
1

The following works for me, testing with TwentyTwelve.

Using load-{$pagenow} instead of admin_init avoids the checking for the global $pagenow. See comments for further info:

// Run only when editing a page
// For new pages load-page-new.php should be used
// See: http://core.trac.wordpress.org/browser/tags/3.5.1/wp-admin/admin.php#L217
add_action( 'load-page.php', 'hide_editor_wpse_88886' );

function hide_editor_wpse_88886() 
{
    // Not really necessary, but just in case
    if( !isset( $_GET['post'] ) )
        return;

    $template = get_post_meta( $_GET['post'] , '_wp_page_template', true );

    if( 'page-templates/front-page.php' == $template )
    {
        remove_post_type_support( 'page', 'editor' );
    }
}

Leave a Comment