Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Created April 26, 2011 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save franz-josef-kaiser/942518 to your computer and use it in GitHub Desktop.
Save franz-josef-kaiser/942518 to your computer and use it in GitHub Desktop.
How to unset certain admin menu groups in wordpress for specific (named) groups of users.
<?php
/*
Plugin Name:Unset Menu Items
Plugin URI: http://example.com
Description:How to unset certain admin menu groups in wordpress for specific (named) groups of users.
Author: Franz Josef Kaiser
Author URI: http://example.com
Version: 0.1
License: extended MIT/Expat license
(c) Copyright 2010-2011 - Franz Josef Kaiser
*/
# How to add a user to a user group from inside your functions.php file
function test_me( $user_group_A )
{
$user_group_A[] = 'Take your own name for testing';
return $user_group_A;
}
# add_filter( 'user_group_A', 'test_me' );
function remove_admin_menu_items( $menu )
{
global $menu, $submenu, $current_user;
get_currentuserinfo();
$parent = get_admin_page_parent();
// don't unset items for admins
#if ( current_user_can( 'manage_options' ) )
#return $menu;
// The following menu item groups contain all main menu items
// If you need submenu items, take a look at the foreach loop $item/$data & use the commented <pre>/print_r
// Menu Item Groups
// Use the filters to adjust them on demand
$menu_itemset_A = array(
'index.php'
,'edit.php'
,'upload.php'
,'link-manager.php'
,'edit.php?post_type=page'
);
$menu_itemset_A = apply_filters( 'menu_itemset_A', $menu_itemset_A );
$menu_itemset_B = array(
'edit-comments.php'
,'themes.php'
,'plugins.php'
,'users.php'
,'tools.php'
,'options-general.php'
);
$menu_itemset_B = apply_filters( 'menu_itemset_B', $menu_itemset_B );
// Separators - in case someone wants to remove them too
$separators = array(
'separator1'
,'separator2'
,'separator-last'
);
// User Groups
// Use the filters to adjust them on demand
$user_group_A = array(
'User Login Name A'
,'User Login Name B'
,'User Login Name C'
,'...'
);
$user_group_A = apply_filters( 'user_group_A', $user_group_A );
$user_group_B = array(
'User Login Name E'
,'User Login Name F'
,'User Login Name G'
,'...'
);
$user_group_B = apply_filters( 'user_group_B', $user_group_B );
foreach ( $menu as $item => $data )
{
// Unset Menu Group items A
// Check if user login name is in group A
if ( in_array( $current_user->user_login, $user_group_A ) )
{
#if ( $parent == $menu[$item][2] )
#wp_die( 'You do not have sufficient permissions to access this page.' );
if ( in_array( $menu[$item][2], $menu_itemset_A ) )
unset( $menu[$item] );
}
// Unset Menu Group items B
// Check if user login name is in group B
elseif ( in_array( $current_user->user_login, $user_group_B ) )
{
#if ( $parent == $menu[$item][2] )
#wp_die( 'You do not have sufficient permissions to access this page.' );
if ( $parent == $menu[$item][2] )
unset( $menu[$item] );
}
// default behavior
else
{
// do something ... or nothing
}
}
// Check if it did what we wanted
# echo '<pre>';
# print_r($menu);
# echo '</pre>';
return $menu;
}
add_filter( 'add_menu_classes', 'remove_admin_menu_items' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment