如果选择了某个模板,是否隐藏页面可视化编辑器?

时间:2011-05-16 作者:Scott

如果当前页面设置为某个模板,如何隐藏页面编辑器(WYSIWYG编辑器)。

当选择某些模板时,我已经在自定义元框中添加了以下代码:

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\');
    }
}
例如,我想要的是,如果$template_file == \'template-page-2quotes.php\'

编辑(工作代码):

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>";
}

4 个回复
最合适的回答,由SO网友:foread 整理而成
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.
       }
    }
}
SO网友:Jonathan Nicol

add_action(\'init\', \'remove_editor_init\');
function remove_editor_init() {
    remove_post_type_support(\'page\', \'editor\');
}
您可以将其封装在现有的逻辑中,以检测当前页面模板,因此您只能对特定页面禁用编辑器。

请参见:http://codex.wordpress.org/Function_Reference/remove_post_type_support

SO网友:Bainternet

您可以使用添加一个简单的CSS规则display:none; 在metabox函数代码中:

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

SO网友:dotancohen

除了给出了非常棒的仅限WP的答案外Advanced Custom Fields 插件只需向其字段定义添加一个指令,即可从关联页面中删除编辑器:

$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
由于这似乎是我所使用的WP商店中一个非常常见的插件,所以应该提到这种方法。

结束

相关推荐

Listing all sub-pages?

我想知道如何编写实际访问页面的子页面列表。因此,我有2页和3个子页:Colors [page] - Red [child of Colors - subpage] - Blue [child of Colors - subpage] - Green [child of Colors - subpage] Numbers [page] - One [child of Numbers - subpage] - Two [child of Numbers