Wordpress - Remove Editor From Homepage
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');
}
}
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
}
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).