7

Im trying to remove the editor from the homepage using the following functions however I am struggling to achieve this?

function hide_homepage_editor() {
    if ( is_admin() ) {
        if (is_front_page()) {
            remove_post_type_support('page', 'editor');
        }
    }
}
add_action( 'admin_init', 'hide_homepage_editor' );

another try:

function hide_homepage_editor() {
    if ( is_admin() ) {
        $post_id = 0;
        if(isset($_GET['post'])) $post_id = $_GET['post'];
        $template_file = get_post_meta($post_id, '_wp_page_template', TRUE);
        if ($template_file == 'front-page.php') {
            remove_post_type_support('page', 'editor');
        }
    }
}
add_action( 'admin_init', 'hide_homepage_editor' );

Anyone know why these are not working and how I can remove the page editor from the page set as frontpage?

1
  • You mean in the admin panel when editing a post which is selected as homepage?
    – Picard
    Jul 14, 2017 at 8:29

3 Answers 3

12

There are a couple of issues with your approach

By using the admin_init hook you won't have any reference to the post object. This means you won't be able to get the post ID or use anything like get_the_ID because the post won't actually be loaded. You can see that in the order here https://codex.wordpress.org/Plugin_API/Action_Reference

So if you run the action hook after the wp action you'll have the post object. For example

add_action('admin_head', 'remove_content_editor');
/**
 * Remove the content editor from ALL pages 
 */
function remove_content_editor()
{ 
    remove_post_type_support('page', 'editor');        
}

Now this snippet will remove the editor from all pages. The problem is that is_home and is_front_page won't work on the admin side so you'll need to add some meta data to distinguish whether you're on the home page. There's a very comprehensive discussion of approaches for that on this page: Best way to present options for home page in admin?

So, if you used some extra meta data, you can then check this like

add_action('admin_head', 'remove_content_editor');
/**
 * Remove the content editor from ALL pages 
 */
function remove_content_editor()
{
    //Check against your meta data here
    if(get_post_meta( get_the_ID(), 'is_home_page' )){      
        remove_post_type_support('page', 'editor');         
    }

}

Hopefully that will help you out

******* Update ************

Actually, I've just looked into this again and realised that there is an easier way. If you have set the front page to be a static page in the Reading settings, you can check against the page_on_front option value. In that case, the following will work

add_action('admin_head', 'remove_content_editor');
/**
 * Remove the content editor from pages as all content is handled through Panels
 */
function remove_content_editor()
{
    if((int) get_option('page_on_front')==get_the_ID())
    {
        remove_post_type_support('page', 'editor');
    }
}
1
  • Thanks for your explanation. Your updated example is exactly what I needed. Thank you :)
    – lky
    Jul 14, 2017 at 9:48
5

Update for Wordpress 5 with Gutenberg:

<?php
/**
 * Disable standard editor and Gutenberg for the homepage
 * keeping the status (enabled/disabled) for others who uses the same filter (i.e. ACF)
 */
add_filter( 'use_block_editor_for_post', 'yourtheme_hide_editor', 10, 2 );
function yourtheme_hide_editor( $use_block_editor, $post_type ) {
    if ( (int) get_option( 'page_on_front' ) == get_the_ID() ) { // on frontpage
        remove_post_type_support( 'page', 'editor' ); // disable standard editor
        return false; // and disable gutenberg
    }

    return $use_block_editor; // keep gutenberg status for other pages/posts 
}
2

thanks for the solution Andrew. I added a code for the page translated by polylang to apply the filter too:

/**
 * Remove the content editor from front page
 */
function remove_content_editor(){
    if((int) get_option('page_on_front')==get_the_ID()){
        remove_post_type_support('page', 'editor');
    }
    if(function_exists("pll_get_post")){
        if((int) pll_get_post(get_the_ID(),"en")==get_the_ID()){
            remove_post_type_support('page', 'editor');
        }
    }
}
add_action('admin_head', 'remove_content_editor');

Change "en" to the matching language string. in my case first language is german and second english (en).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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