Tuesday, 25 December 2012

Hiding unnecessary menus in wordpress admin


In this post, I describe a technique for hiding unnecessary menus. 



Some time we need to hind unnecessary menus for other clients role just like (administrator, contributor, subscriber, author, editor, etc) then with the help of this function we can hide wordpress menu.
1.    
1   This code include your themes>>functions.php files.

// remove unnecessary menus
function remove_admin_menus () {
                global $menu;
                                $restrict = explode(',', 'contributor');// you can pass user role (administrator, contributor, subscriber, author, editor, etc)
                $restrict_user = explode(',', 'Posts,Comments,Media,Profile,Appearance,Plugins,Users, Tools,Settings');// you can pass menus name whose you want to hide.
               
                $f = create_function('$v,$i', 'return __($v);');
                array_walk($restrict, $f);
                if (!current_user_can('activate_plugins')) {
                                array_walk($restrict_user, $f);
                                $restrict = array_merge($restrict, $restrict_user);
                }
                // remove menus
                end($menu);
                while (prev($menu)) {
                                $k = key($menu);
                                $v = explode(' ', $menu[$k][0]);
                                if(in_array(is_null($v[0]) ? '' : $v[0] , $restrict)) unset($menu[$k]);
                }
}
add_action('admin_menu', 'remove_admin_menus');

No comments:

Post a Comment