0
votes

I need to hide / remove a menu and submenu in WordPress backend.

Main menu item:

admin.php?page=themeit_dashboard

Sub menu items:

admin.php?page=themeit_dashboard&tab=builder
admin.php?page=_options&tab=1
admin.php?page=themeit_dashboard&tab=license

I have tried the code below code, but that does not work. Two of the subpages link to the same page but show different tabs.

function remove_menus(){
    if ( !current_user_can( 'manage_options' ) ) {
        remove_menu_page( 'admin.php?page=themeit_dashboard' );                                                         //themeit
        remove_submenu_page( 'admin.php?page=themeit_dashboard', 'admin.php?page=themeit_dashboard&tab=builder' );      //themeit
        remove_submenu_page( 'admin.php?page=_options', 'admin.php?page=_options&tab=1' );                              //themeit
        remove_submenu_page( 'admin.php?page=themeit_dashboard', 'admin.php?page=themeit_dashboard&tab=license' );      //themeit
    }
}
add_action( 'admin_menu', 'remove_menus' );
1
Could you just hide via css? If that would work for you, you could add a editor-style.css to your theme (codex.wordpress.org/Editor_Style), find the id or class of the menus/submenus you want to hide, and remove via display:none. - scmccarthy22
Will it be possible to it with CSS and have different menus on each user role (admin, editor etc.). @scmccarthy22 - VanDerPrygel
try adding third arg priority like add_action( 'admin_menu', 'remove_menus', 99 ); - Shravan Sharma
@ShravanSharma Unfortunately that does not help. I am almost certain that I am making a mistake regarding the remove_submenu_page. ('admin.php?page=_options', 'admin.php?page=_options&tab=1'). - VanDerPrygel

1 Answers

0
votes

You can do it by using current_user_can() and remove_menu_page, as you tried to do.

But It can be done by without coding too.... long back I was using a plugin called - "Advanced Access Manager". Use that or any similar plugins.

With those you will be able to assign permisson on menus based on roles with a cool UI.

I personally did found it very easy.