Remove Editor From Homepage

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?

3 s
3

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

Leave a Comment