wearerequired / admin-menu-manager

Manage the WordPress admin menu using a simple drag & drop interface.
https://wordpress.org/plugins/admin-menu-manager/
GNU General Public License v2.0
61 stars 6 forks source link

Overrides remove_menu_page #37

Open blogmom opened 7 years ago

blogmom commented 7 years ago

With 2.0.0.-alpha: In my functions.php, I check if a user is an admin, and if not, then I hide all Admin Panel items using remove_menu_page. However, when AMM is activated, certain items reappear: Home, All Posts, All Comments, Available Tools. This is consistent and repeatable. When AMM plugin is deactivated, the items are again hidden. Here is my code, pop it into your functions.php and you'll see what I mean. With that code in your functions.php:

  1. deactivate AMM plugin and login as a non-admin user, navigate to wp-admin and notice the panel is empty.
  2. activate AMM plugin and refresh, notice the admin panel is no longer empty.
add_action('admin_menu', 'remove_admin_sidebar_from_endusers'); 
function remove_admin_sidebar_from_endusers ()  { 
    get_currentuserinfo();
    global $user_level;
    if ( $user_level <= 8 ) {
        remove_menu_page( 'index.php' );                  //Dashboard
        remove_menu_page( 'profile.php' );                //Profile     
        remove_menu_page( 'jetpack' );                    //Jetpack* 
        remove_menu_page( 'edit.php' );                   //Posts
        remove_menu_page( 'upload.php' );                 //Media
        remove_menu_page( 'edit.php?post_type=page' );    //Pages
        remove_menu_page( 'edit-comments.php' );          //Comments
        remove_menu_page( 'themes.php' );                 //Appearance
        remove_menu_page( 'plugins.php' );                //Plugins
        remove_menu_page( 'users.php' );                  //Users
        remove_menu_page( 'tools.php' );                  //Tools
        remove_menu_page( 'options-general.php' );        //Settings
        //echo "<script type='text/javascript' >document.body.className+=' folded';</script>";  //collapse by default
        remove_meta_box('dashboard_right_now', 'dashboard', 'core');
        remove_meta_box('dashboard_activity', 'dashboard', 'core');
        remove_meta_box('dashboard_recent_comments', 'dashboard', 'core');
        remove_meta_box('dashboard_incoming_links', 'dashboard', 'core');
        remove_meta_box('dashboard_plugins', 'dashboard', 'core');
        remove_meta_box('dashboard_quick_press', 'dashboard', 'core');
        remove_meta_box('dashboard_recent_drafts', 'dashboard', 'core');
        remove_meta_box('dashboard_primary', 'dashboard', 'core');
        remove_meta_box('dashboard_secondary', 'dashboard', 'core');        
    } 
} 
swissspidy commented 7 years ago

Interesting, thanks for your report!

Right now the plugin hooks into admin_menu as well and basically tries to build a new menu from scratch. Since the complete menu is stored in an option, the removed pages are still in there and need to be removed somehow.

I suspect that Parent_Menu_Iterator::is_top_level_item() correctly returns false because the menu pages do not exist anymore. However, Parent_Menu_Iterator::is_submenu_item() might return true in that case because every parent menu item has a child with the same name.

Parent_Menu_Iterator::is_submenu_item() exists because you're able to move a child menu item to the top level and this needs to be detected somehow. Perhaps this detection needs to be done better so that we can distinguish between moved and deleted menu items.