How to hide pages in the WordPress admin

Sometimes it’s handy to hide pages in the WordPress admin from certain users. For example, when using WordPress as a CMS, where you define a static front page and a Posts page for all the posts, you might not want users trying to edit the Posts page since their edits won’t show up on the site anyway. A common scenario I see is users trying to edit the dummy “News” page assigned as the Posts page, or trying to change the front page of the site by editing the dummy “Home” page.

You can’t blame the users for trying to add news to their site by editing the “News” page. It makes sense. What doesn’t make sense is how WordPress does things in this regard, but that’s another discussion. To prevent this confusion, we can hide these dummy pages, or any other pages we want, from the users.

Using the code below, you can hide specific pages from all users who aren’t Administrators. It’s worth noting that this isn’t a security solution. It’s a simple piece of code for preventing confusion or accidents. If you need a real security solution, I recommend something like BU Section Editing by the fine folks at Boston University.

<?php
/*
Plugin Name: Exclude pages from admin
Plugin URI: https://www.johnparris.com/
Description: Removes pages from admin that shouldn't be edited.
Version: 1.0
Author: John Parris
Author URI: https://www.johnparris.com/
License: GPLv2
*/

function jp_exclude_pages_from_admin($query) {

	if ( ! is_admin() )
		return $query;

	global $pagenow, $post_type;

	if ( !current_user_can( 'administrator' ) && $pagenow == 'edit.php' && $post_type == 'page' )
		$query->query_vars['post__not_in'] = array( '10', '167', '205' ); // Enter your page IDs here
 

}
add_filter( 'parse_query', 'jp_exclude_pages_from_admin' );

Someone brought up the fact that this doesn’t remove the Edit Page link from the admin bar on the front end of the site. This is true. It’s worth mentioning again that this isn’t a security solution. It’s a simple piece of code to keep people from getting confused or accidentally messing something up. Having said that, here’s a little piece of code for hiding the Edit Page link.

// Remove the Edit link from the admin bar
function jp_remove_admin_bar_edit_link() {

	if( ! current_user_can( 'administrator' ) ) {
		
		global $wp_admin_bar;
		$wp_admin_bar->remove_menu( 'edit' );
	}
}
add_action( 'wp_before_admin_bar_render', 'jp_remove_admin_bar_edit_link' );

Feel free to combine the two pieces of code. For more details about the Roles and Capabilities of WordPress, see this page.

24 thoughts on “How to hide pages in the WordPress admin

  1. Hi! Thanks so much for this code! It is very useful to me. I was using this solution until I upgraded to WP 3.8. Now no pages are hidden anymore. Is this code working for you in 3.8 on twenty fourteen with no plugins activated? Thanks for your help.

  2. Sarah, I just tested it and it’s working for me. Are you testing it under a non-admin account?

    FYI, I just made some minor mods to the post to clean up some odd characters in the white space and removed some unnecessary braces. Maybe try copying it again to see if those funky characters were a problem.

    • Thanks for the reply. I thought I was testing under a non-admin — actually, it was a custom role I made (“director”). would that create an issue? I will test again and let you know.

  3. The custom role shouldn’t matter. Also note you can change the capability defined at current_user_can to be something else if needed. See here. Also worth mentioning, the code displayed above applies to pages only. If you need another post type, you’d have to pass that in.

    • Yes, those are all tricky parts. It does work now! I think what happened was I switched to twenty-fourteen theme to test something else, and forgot that this code was in the now inactive theme’s functions.php file. I was testing so many things, forgot which was which. Thanks so much for your prompt replies! I am to launch today.

  4. Alexa says:

    Is there any plugin that does this more easily? I’m a little wary of coding but have a website where I am the admin and they really only need to manage the content of about 6 pages. I want to hide all the others that are more structural to the website so that there is no chance they change those. I’d like to see them as the Site-Wide Admin, but have assigned them as Editors so I would have thought there would be an easy plugin for this? I can see all sorts of other “hide” options, but not a simple one to just hide view of certain pages. Thanks for your help –

  5. Alexa says:

    OK – I’m back – I gave it a go (yikes!) and was able to create the plugin (thank goodness for middle of the night on the east coast so the error codes don’t get seen! I changed it to editor and then worked like a charm when I logged into the website as an editor… except, they are still missing when I logged back in as the admin. How would I change the code so that the pages are *only* hidden for Editors? Thanks!

    Here’s what I changed it to (not necessarily correct – just a stab in the dark!) Thank for the help!

    query_vars[‘post__not_in’] = array( ‘450’, ‘452’, ‘454’, ‘446’, ‘503’, ‘472’, ‘456’, ‘448’ ); // Enter your page IDs here

    }
    add_filter( ‘parse_query’, ‘jp_exclude_pages_from_admin’ );

  6. Hey Alexa, the code in the post should work as is to hide pages from Editors while leaving them visible to you as Administrator. You should only need to change the page IDs. If it didn’t work like that, someone may have customized some of the default WordPress roles. In that case, it’s hard to say, but you could try to change ‘administrator’ to ‘manage_options’ and give it a go.

  7. isaaccarr1 says:

    For anyone who wants to do this but using page names instead of id’s, here’s my simple solution: http://pastebin.com/86nELBGe
    I also changed the chunk which removes the “edit” link to only run on the specified page.

    ( I removed the user roles conditions, since they weren’t important to me, but you can add those in again if you like).

Leave a Reply