Disabling editor for specific page

I’m trying to disable the editor for a specific page, namely the front-page. I found this code:

add_action( 'admin_init', 'hide_editor' );
 
function hide_editor() {
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    if( !isset( $post_id ) ) return;
 
    $template_file = get_post_meta($post_id, '_wp_page_template', true);
     
    if($template_file == 'submit.php'){ // edit the template name
        remove_post_type_support('page', 'editor');
    }
}

So I tried (in ./app/admin.php):

add_action('admin_init', function () {
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    if( !isset( $post_id ) ) return;
 
    $template_file = get_post_meta($post_id, '_wp_page_template', true);
     
    if($template_file == 'front-page.php') {
        remove_post_type_support('page', 'editor');
    }
});

And it doesn’t work, but a more simple:

add_action('admin_init', function () {
    remove_post_type_support('page', 'editor');
});

It does successfully disable the editor for ALL pages.

I’m pretty new still to writing functions the modern/Sage way, so not sure what went wrong?

Also, is it best practice to place this sort of function within ./app/admin.php?

You’re misunderstanding what _wp_page_template represents. The code above will never return true (well, technically it might in a very gross edge case, but that’s definitely not the behavior you’re looking for).

WordPress calls a bunch of different things “templates” which gets confusing, but:

_wp_page_template refers only to what WordPress calls “Custom Page Templates.” It will contain the name (or more accurately, the path) of the file selected from the “Page Template” dropdown, if one is selected. The front-page.php “template” is not a Custom Page Template–it’s a specially named file that WordPress will load for the front page.

  • WordPress decides to load front-page.php for the page that is chosen as the front page. This is determined at runtime.
  • WordPress is told to load the file stored in _wp_page_template (if any) for a given page. That data is stored in the database.

If you want to determine whether a page you’re editing in the admin is the front page, something like this is probably a better bet:

$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
if( !i sset( $post_id ) ) return;
if ( get_option( 'page_on_front' ) == $post_id ) {
  // this is the front page
}

Thank you so much (again) for the response. That does make sense, but I’m getting an error with the line:

$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;

It’s telling me “Undefined index: post”.

The full updated function as I have it in ./app/admin.php is:

add_action('admin_init', function () {
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
    if( !isset( $post_id ) ) return;
    if ( get_option( 'page_on_front' ) == $post_id )
        remove_post_type_support('page', 'editor');
});

This is a PHP error that has nothing to do with Sage. What have you tried in order to fix it? Have you looked at the array that’s missing that index? What is that line of code trying to do, and why might it be failing?

My understanding is its going to set $post_id to whatever $_GET[‘post’] returns or otherwise if null then will return the post’s post_id. If it has a post_id, i.e., a post, do nothing more (return).

But somehow it is not assigning a $post_id variable and even worse than that it’s also not accessing the index properly somehow? That part stumps me for the time being. It has something to do with the namespace perhaps?

I’ve also found another simpler stack overflow solution that seems straight forward but simply doesn’t work for me either. They propose:

    if((int) get_option('page_on_front')==get_the_ID())
    {
        remove_post_type_support('page', 'editor');
    }

I do not get an error with that, but it simply doesn’t disable the editor for the front-page (or any page).

EDIT: And strange to say once I put { } around the previous if statement it suddenly starting working although I cannot say why. I was under the impression the { } is normally optional? Anyhow here is the correct syntax for anyone wishing to do the same in the future:

./app/admin.php

add_action('admin_init', function () {
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
    if( !isset( $post_id ) ) return;
    if ( get_option( 'page_on_front' ) == $post_id ) {
        remove_post_type_support('page', 'editor');
    }
});

Thanks much @alwaysblank once again.

And I think I spoke too soon. The latest status is now it is working as desired, however, I can’t login to the dashboard or anywhere else in the admin without confronting this error "“Undefined index: post” pointing to the line:

$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];

I would be pleased if anyone had any suggestions, but otherwise I’ll plug away at it until I can find the correct way to do it and report back here once I do.

This topic was automatically closed after 42 days. New replies are no longer allowed.