Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>";
}
share|improve this question
    
Bear in mind it is 7am on a Monday and I've just started my coffee! The easiest solution I think of first thing, is to make a custom post type for these posts. Then you can just not add editor to the supports options. But I'm guessing you have reasons for not wanting a CPT possibly? –  Rev. Voodoo May 16 '11 at 11:11
    
Sorry custom post type wont work here as I'm using pages rather than posts. I'm aware in posts you can use 'supports' => array('editor') I was wondering if there is a filter or hook that you can trigger to remove the editor... –  Brady May 16 '11 at 11:15

3 Answers 3

up vote 5 down vote accepted
add_action('init', 'remove_editor_init');
function remove_editor_init() {
    // if post not set, just return 
    // fix when post not set, throws PHP's undefined index warning
    if (isset($_GET['post'])) {
        $post_id = $_GET['post'];
    } else if (isset($_POST['post_ID'])) {
        $post_id = $_POST['post_ID'];
    } else {
        return;
    }
    $template_file = get_post_meta($post_id, '_wp_page_template', TRUE);
    if ($template_file == 'page-your-template.php') {
        remove_post_type_support('page', 'editor');
    }
}
share|improve this answer
    
+1. Still, I'd suggest adding a few explanatory lines to an answer - makes for better readability and understandability of the answer... –  Johannes Pille Mar 20 '13 at 17:10
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

share|improve this answer

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>';
}
share|improve this answer
    
Thanks Bainternet. I've taken your idea and worked it diferently. See edit –  Brady May 16 '11 at 11:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.