Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I used the following to remove certain menus for the "editor"

function remove_menus () {
        global $menu;

        if( (current_user_can('install_themes')) ) { 
            $restricted = array(); } // check if admin and hide nothing
            else { // for all other users

                // Remove Tools, Appearance, Links, Plugins, Comments,
                if ($current_user->user_level < 10)
                $restricted = array(__('Posts'),  __('Links'), __('Appearance'), __('Tools'), __('Settings'), __('Comments'), __('Plugins')); // this removes a lot! Just delete the ones you want to keep showing
                end ($menu);
                while (prev($menu)){
                    $value = explode(' ',$menu[key($menu)][0]);
                    if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
                }
            }
        }

    add_action('admin_menu', 'remove_menus');

This works except now the client has asked to remove the following:

On the dashboard, remove the "Add New Page".

enter image description here

Under the "pages" tab, remove the "Add New Page":

enter image description here

Is this possible?

share|improve this question
    
Can you not just change permissions on the user? –  m0r7if3r Apr 2 '12 at 20:49
    
?? Does changing the permissions remove specific items from the menus? –  redconservatory Apr 2 '12 at 20:51
    
What permission level would remove "Add Pages"? Writer? –  redconservatory Apr 2 '12 at 20:52
1  
Not roles, permissions. As in disallow edit_pages or something. Just hiding them on the menu like you're doing (as far as I'm aware at least) is merely security by obscurity. –  m0r7if3r Apr 2 '12 at 20:58

3 Answers 3

up vote 2 down vote accepted

A much simpler solution is to use remove_menu_page , remove_submenu_page and remove_menu (admin bar).

Your example of removing the sub-menu item for "add new" would be

remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' );

To remove it from the admin menu bar you can use the example in @BandonRandon's post.

share|improve this answer

Please note that there's no need to mess around with menu globals anymore. Since WP 3.1 (or thereabouts) we have 2 new functions to play with, which let you remove any page from the menu:

remove_submenu_page( $menu_slug, $submenu_slug );
remove_menu_page( $menu_slug );

An example usage would be this:

remove_submenu_page( 'edit.php', 'post-new.php' );

To restrict access to a page without the use of a plugin (depending on the situation it might be overkill) you can use the $pagenow global:

function my_restrict_access() {
    global $pagenow;

    if( $pagenow == 'post-new.php' && ! current_user_can( 'publish_posts' ) )
          wp_redirect( admin_url() );
}
add_action( 'admin_init', 'my_restrict_access', 0 );
share|improve this answer
1  
Your example also applies to posts (I think) , to specify pages you can add ?post_type=page. –  Wyck Apr 3 '12 at 0:41

I agree with m0r7if3r you'll want to use a role editing plugin to make it so the user can't add/edit pages. Once that is done you can do something like this to hide the menu.

add_action( 'admin_menu' , 'wpse_47731_remove_admin_menus',11 );
add_action( 'wp_before_admin_bar_render', 'wpse_47731_remove_admin_bar_links' );
add_action('admin_head','wpse_47731_hide_admin_buttons');

function wpse_47731_remove_admin_menus() {
    if (!current_user_can( 'edit_pages' ) ) {
        //pages
        global $menu;
        global $submenu;
        unset( $menu[75] );
        unset($submenu['edit.php?post_type=page'][10][1]);
    }
     else {
        return;
    }
}

function wpse_47731__hide_admin_buttons(){
  global $current_screen;
  if (!current_user_can( 'edit_pages' ))     {
        if ( ($current_screen->id == 'edit-page' ) || ( $current_screen->id == 'page'))
            {
               echo '<style>.add-new-h2,#add_gform{display:none;}</style>';  
        }
        else if( ($current_screen->id == 'edit-post' ) || ( $current_screen->id == 'post'))
           {
            echo '<style>#add_gform {display:none;}</style>';  

        }
    else{
        return;
    }

}
}

function wpse_47731_remove_admin_bar_links() {
    if (!current_user_can( 'edit_pages' ) ) {
        global $wp_admin_bar;
        $wp_admin_bar->remove_menu('new-page');
    }
}

That's basicly the code I use I'm assuming you may already be hooking into these with your function but you'll have to play with it a bit. When I originally did it I found a super good article on Google explaining how it all worked but I have lost the bookmark but if I find it I'll make sure to post.

Here is Justin Tadlocks post (which is pretty good) http://justintadlock.com/archives/2011/06/13/removing-menu-pages-from-the-wordpress-admin

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.