禁用特定页面模板的WP编辑器

时间:2017-02-11 作者:passoa

答案是this question 工作得很好,但我想在使用其他模板时也排除编辑器。您能告诉我如何扩展代码,使其能够与多个模板一起工作吗?

2 个回复
最合适的回答,由SO网友:user6552940 整理而成

是的,试试这个:

function remove_editor() {
    if (isset($_GET[\'post\'])) {
        $id = $_GET[\'post\'];
        $template = get_post_meta($id, \'_wp_page_template\', true);
        switch ($template) {
            case \'template_01.php\':
            case \'template_02.php\':
            case \'template_03.php\':
            case \'template_04.php\':
            // the below removes \'editor\' support for \'pages\'
            // if you want to remove for posts or custom post types as well
            // add this line for posts:
            // remove_post_type_support(\'post\', \'editor\');
            // add this line for custom post types and replace 
            // custom-post-type-name with the name of post type:
            // remove_post_type_support(\'custom-post-type-name\', \'editor\');
            remove_post_type_support(\'page\', \'editor\');
            break;
            default :
            // Don\'t remove any other template.
            break;
        }
    }
}
add_action(\'init\', \'remove_editor\');
更改\'template_01.php\' ... \'template_04.php\' 使用模板名称,如果需要,可以通过添加更多案例来添加更多模板名称
例如。

case \'template_05.php\':
然而,上述代码和answer 要求您首先从页面编辑屏幕设置页面模板<我希望这有助于并明确这是如何工作的。

SO网友:Jankyz

使用ACF pluginFirstly删除自定义设置中所选页面的编辑器

/*
 * Add additional settings menu related with ACF plugin
 */
if ( function_exists( \'acf_add_options_page\' ) ) {
    acf_add_options_page( array(
        \'page_title\' => \'Add. settings of Theme\',
        \'menu_title\' => \'Add. settings\',
        \'menu_slug\'  => \'theme-general-settings\',
        \'capability\' => \'edit_posts\',
        \'redirect\'   => false
    ) );
下一步是创建与ACF plugin和final中的页面相关的自定义字段

/*
 * Remove editor for pages chosen in custom settings, working with ACF plugin
 */
function ogate_remove_editor() {
    $pages_without_editor = get_field(\'pages\', \'option\');
    if (isset($_GET[\'post\'])) {
        $id = $_GET[\'post\'];
        $template = get_post_meta($id, \'_wp_page_template\', true);
        if(!empty($pages_without_editor)) {
            foreach ( $pages_without_editor as $single ) {
                $my_template = get_post_meta($single->ID, \'_wp_page_template\', true);
                $template == $my_template ?
                    remove_post_type_support(\'page\', \'editor\') : false;
            }
        }
    }
}
add_action(\'init\', \'ogate_remove_editor\');