Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hide the content editor for certain pages in WordPress
<?php
/**
* Hide editor on specific pages.
*
*/
add_action( 'admin_init', 'hide_editor' );
function hide_editor() {
// Get the Post ID.
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
if( !isset( $post_id ) ) return;
// Hide the editor on the page titled 'Homepage'
$homepgname = get_the_title($post_id);
if($homepgname == 'Homepage'){
remove_post_type_support('page', 'editor');
}
// Hide the editor on a page with a specific page template
// Get the name of the Page Template file.
$template_file = get_post_meta($post_id, '_wp_page_template', true);
if($template_file == 'my-page-template.php'){ // the filename of the page template
remove_post_type_support('page', 'editor');
}
}
@nimmolo

This comment has been minimized.

Copy link

commented Aug 25, 2013

big up, many thanks. this works great.

@nimmolo

This comment has been minimized.

Copy link

commented Jun 3, 2014

Hi Ramsey,
I just had debug on and noticed the above code returns php errors, because the admin_init hook is too early - it needs to be on admin_head. Also, the post object is not attainable outside the edit page, so i added a conditional to check for that.

If you modify the top of your function it resolves the errors.

for reference [http://stackoverflow.com/questions/8463126/how-to-get-post-id-in-wordpress-admin]
Thanks again for the code.

add_action( 'admin_head', 'hide_editor' );

function hide_editor() {
    global $pagenow;
    if( !( 'post.php' == $pagenow ) ) return;

    global $post;
    // Get the Post ID.
    $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    if( !isset( $post_id ) ) return;
    ...
@gnowland

This comment has been minimized.

Copy link

commented Jun 27, 2014

@nimmolo you rock man, Ramsey's code is in like 8 google hits and only you were diligent enough to catch the debug error and had the fix. Wish you could push this to everyone! 👍

@juliancoates

This comment has been minimized.

Copy link

commented Mar 16, 2015

thanks, really useful!

@atomtigerzoo

This comment has been minimized.

Copy link

commented Jul 16, 2015

Nice & thank you :) I forked your gist giving it some the functionality to define the page-title and page-templates other than directly in the function: https://gist.github.com/atomtigerzoo/0dd49ed9ca67ec111465

@Frique

This comment has been minimized.

Copy link

commented Jan 10, 2016

A cleaner way to get the template file (without relying on global variables and custom get_post_meta calls) is using get_page_template() which will return the full path that you can then filter to only get the php file:

$template_file = substr( get_page_template(), strrpos( get_page_template(), '/' ) + 1 );
@glennnall

This comment has been minimized.

Copy link

commented Feb 16, 2016

thanks both of you - just stuck this in my child-theme functions.php (with admin_head) and it did just what i wanted it to. awesome.

@bryanwillis

This comment has been minimized.

Copy link

commented Feb 28, 2016

@nimmolo is correct here that the admin_init hook is too early. Not only because of the errors but certain metaboxes will not get removed. The preferred hook is actually do_meta_boxes, but admin_head or admin_menu would probably work as well.

@Hollyw00d

This comment has been minimized.

Copy link

commented Mar 4, 2016

This worked perfectly for me. Thank you ramseyp!

@praghavaji

This comment has been minimized.

Copy link

commented Jun 24, 2016

Thanks. Very awesome.

@olirabt

This comment has been minimized.

Copy link

commented Sep 23, 2016

Hello, thanks, this works perfectly. However, I was unable to disable the editor also on the translated pages in my admin. I use polylang plugin in order to translate my pages. The editor is disabled on the original page, but it is abled on the pages I use for traductions.
Could you please help me in hiding the editor on all pages ?
Thanks

@cramdesign

This comment has been minimized.

Copy link

commented Oct 8, 2016

possibly a cleaner way still to get the name of the template file:

$template_file = basename( get_page_template() );
@RyuuZaky

This comment has been minimized.

Copy link

commented Nov 12, 2016

Thanks bro, really useful 👍

@endymion1818

This comment has been minimized.

Copy link

commented Mar 23, 2017

Thanks to both of you, really helped me too.

@jordanboston

This comment has been minimized.

Copy link

commented May 26, 2017

Thanks! This seemed to work well for me, from the combined comments above:

add_action( 'admin_head', 'hide_editor' );
function hide_editor() {
	$template_file = $template_file = basename( get_page_template() );
	if($template_file == 'template.php'){ // template
		remove_post_type_support('page', 'editor');
	}
}

It seems simpler, and I have not seen any issues.

@WebAssembler

This comment has been minimized.

Copy link

commented Jun 16, 2017

@jordanboston you have written $template file twice on line 3:

$template_file = $template_file = basename( get_page_template() );

@bluefractals

This comment has been minimized.

Copy link

commented Mar 25, 2018

Is there a way to move it below other fields on the page? I'm using Advanced Custom Fields and would like to move the default content area below the ACF fields.

@billyranario

This comment has been minimized.

Copy link

commented Apr 19, 2018

Very helpful!

@roborracle

This comment has been minimized.

Copy link

commented May 30, 2018

@jordanboston - that worked perfectly - thank you.

@ghost

This comment has been minimized.

Copy link

commented Jun 18, 2018

Thanks to @ramseyp, @jordanboston and @WebAssembler - works great.

@edurodriguesdias

This comment has been minimized.

Copy link

commented Aug 2, 2018

Thanks bro!

@ncole458

This comment has been minimized.

Copy link

commented Sep 11, 2018

Nice one thanks! I needed to add remove_post_type_support('post', 'editor'); to remove from posts also FYI.

@MikeNGarrett

This comment has been minimized.

Copy link

commented Jan 4, 2019

Make sure you check to see if you're on the right admin screen by using get_current_screen().

@anthonyabraira

This comment has been minimized.

Copy link

commented Mar 6, 2019

What if the template files are in a folder like:

page-sections/page-hero-banner.php

I tried this to no avail:

/**
 * Hide editor on specific pages.
 *
 */
add_action( 'admin_init', 'hide_editor' );
function hide_editor() {
  // Get the Post ID.
  $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
  if( !isset( $post_id ) ) return;

  // Hide the editor on a page with a specific page template
  // Get the name of the Page Template file.
  $template_file = get_post_meta($post_id, '_wp_page_template', true);

  
  if($template_file == 'page-section/page-event-feature.php'){ // the filename of the page template
    remove_post_type_support('page', 'editor');
  }
}

@emmadalby

This comment has been minimized.

Copy link

commented Jul 3, 2019

Very helpful. Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.