Wednesday, 22 April 2015

Wordpress Disable Admin Panel Menus Based on Role

In wordpress admin panel, if you want to hide certain menu's for non-admins , then add the below code in your current theme's functions.php file.

function remove_menus () {
global $menu;

        $restricted = array(__('Dashboard'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'),  __('Plugins'));
        end ($menu);
        while (prev($menu)){
            $value = explode(' ',$menu[key($menu)][0]);
            if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
      /* To hide menus for non -admin users*/
add_action('admin_menu', 'remove_menus');