最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

admin - Disable WP Editor for specific page templates

programmeradmin3浏览0评论

The answer to this question is working great but I want to exclude the editor also when other templates are used. Can you please tell me how to extend the code to make this work with more than one template?

The answer to this question is working great but I want to exclude the editor also when other templates are used. Can you please tell me how to extend the code to make this work with more than one template?

Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Feb 11, 2017 at 10:33 passoapassoa 231 gold badge1 silver badge4 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 8

Yes, try this :

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');

Change the 'template_01.php' ... 'template_04.php' with your template names and if you want you can add more template names by adding more cases.
e.g.

case 'template_05.php':


However, the above code and the code from the answer requires you to first set the page templates from the page edit screen.
I hope this helps and clears how this works.

Remove editor for pages chosen in custom settings, working with ACF plugin Firstly

/*
 * 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
    ) );

next step is to create custom fields with relation to pages in ACF plugin and in the 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');
发布评论

评论列表(0)

  1. 暂无评论