Hide page visual editor if certain template is selected?

How do I hide the page editor (WYSIWYG editor) if the current page is set to a certain template.

I have the following code already to add in custom meta boxes when certain templates are chosen:

add_action('admin_init','my_meta_init');
function my_meta_init()
{
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
    $template_file = get_post_meta($post_id, '_wp_page_template', TRUE);

    $savemeta = true;

    if ($template_file == 'template-page-2quotes.php') {
        add_meta_box('main_quote_meta-meta', 'Top Quote', 'main_quote_meta', 'page', 'side', 'low');
        add_meta_box('sub_quote_meta-meta', 'Right Hand Side Quote', 'sub_quote_meta', 'page', 'normal', 'low');
    } elseif ($template_file == 'template-page-1quote.php') {
        add_meta_box('sub_quote_meta-meta', 'Right Hand Side Quote', 'sub_quote_meta', 'page', 'normal', 'low');
    } elseif ($template_file == 'template-page-factsnfigures.php') {
        add_meta_box('facts_n_figures-meta', 'Amount Raised', 'facts_n_figures', 'page', 'normal', 'low');
    } elseif ($template_file == 'template-page-fundraising.php') {
        add_meta_box('fundraising_ideas-meta', 'Fundraising Ideas', 'fundraising_ideas', 'page', 'side', 'low');
    } else {
        $savemeta = false;  
    }
    if($savemeta == true) {
        add_action('save_post','my_meta_save');
    }
}

What I would like for example is that the editor is removed if $template_file == 'template-page-2quotes.php'

Edit (Working Code):

add_action('admin_init','my_meta_init');
function my_meta_init()
{
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
    $template_file = get_post_meta($post_id, '_wp_page_template', TRUE);

    $savemeta = true;
    $hideeditor = false;

    if ($template_file == 'template-page-2quotes.php') {
        add_meta_box('main_quote_meta-meta', 'Top Quote', 'main_quote_meta', 'page', 'side', 'low');
        add_meta_box('sub_quote_meta-meta', 'Right Hand Side Quote', 'sub_quote_meta', 'page', 'normal', 'low');
    } elseif ($template_file == 'template-page-1quote.php') {
        add_meta_box('sub_quote_meta-meta', 'Right Hand Side Quote', 'sub_quote_meta', 'page', 'normal', 'low');
    } elseif ($template_file == 'template-page-factsnfigures.php') {
        add_meta_box('facts_n_figures-meta', 'Amount Raised', 'facts_n_figures', 'page', 'normal', 'low');
    } elseif ($template_file == 'template-page-fundraising.php') {
        add_meta_box('fundraising_ideas-meta', 'Fundraising Ideas', 'fundraising_ideas', 'page', 'side', 'low');
    } elseif($template_file == 'template-page-news.php') {
        $hideeditor = true;
        $savemeta = false;
    } else {
        $savemeta = false;  
    }
    if($savemeta == true) {
        add_action('save_post','my_meta_save');
    }
    if($hideeditor == true) {
        add_action('admin_print_styles', 'admin_no_editor_style');
    }
}
function admin_no_editor_style() {
    echo "<style>#postdivrich{display:none;}</style>";
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

add_action( 'init', 'remove_editor_init' );

function remove_editor_init() {
    // If not in the admin, return.
    if ( ! is_admin() ) {
       return;
    }

    // Get the post ID on edit post with filter_input super global inspection.
    $current_post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
    // Get the post ID on update post with filter_input super global inspection.
    $update_post_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_NUMBER_INT );

    // Check to see if the post ID is set, else return.
    if ( isset( $current_post_id ) ) {
       $post_id = absint( $current_post_id );
    } else if ( isset( $update_post_id ) ) {
       $post_id = absint( $update_post_id );
    } else {
       return;
    }

    // Don't do anything unless there is a post_id.
    if ( isset( $post_id ) ) {
       // Get the template of the current post.
       $template_file = get_post_meta( $post_id, '_wp_page_template', true );

       // Example of removing page editor for page-your-template.php template.
       if (  'page-your-template.php' === $template_file ) {
           remove_post_type_support( 'page', 'editor' );
           // Other features can also be removed in addition to the editor. See: https://codex.wordpress.org/Function_Reference/remove_post_type_support.
       }
    }
}

Method 2

add_action('init', 'remove_editor_init');
function remove_editor_init() {
    remove_post_type_support('page', 'editor');
}

You can wrap this inside your existing logic for detecting the current page template, so you only disable the editor for specific pages.

See: http://codex.wordpress.org/Function_Reference/remove_post_type_support

Method 3

you can add a simple CSS rule with display:none; in your metabox function code:

if ($template_file == 'template-page-2quotes.php') {
   echo '<style>#postdivrich{display:none;}</style>';
}

Method 4

In addition to the terrific WP-only answers given, users of the Advanced Custom Fields plugin can simply add a single directive to their field definitions the remove the Editor from associated pages:

$args = array(); // Already defined with many elements

$args['hide_on_screen'] = array('the_content'); // Remove the Editor from associated Pages

acf_add_local_field_group($args); // Already defined for use with extant $args

As this seems to be an extraordinarily common plugin at the WP shops that I’ve worked with, this method should be mentioned.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x