Skip to content

Instantly share code, notes, and snippets.

@mitchellhislop
Created March 5, 2011 04:40
Show Gist options
  • Save mitchellhislop/856121 to your computer and use it in GitHub Desktop.
Save mitchellhislop/856121 to your computer and use it in GitHub Desktop.
function remove_menus () {
global $menu, $submenu, $user_ID;
$the_user = new WP_User($user_ID);
$valid_page = "admin.php?page=contact-form-7/admin/admin.php";
$restricted = array('index.php','edit.php','categories.php','upload.php','link-manager.php','edit-pages.php','edit-comments.php', 'themes.php', 'plugins.php', 'users.php', 'profile.php', 'tools.php', 'options-general.php');
$restricted_str = 'widgets.php';
end ($menu);
while (prev($menu)){
$menu_item = $menu[key($menu)];
$restricted_str .= '|'.$menu_item[2];
if(in_array($menu_item[2] , $restricted)){
$submenu_item = $submenu[$menu_item[2]];
if($submenu_item != NULL){
$tmp = $submenu_item;
$max = array_pop(array_keys($tmp));
for($i = $max; $i > 0;$i-=5){
if($submenu_item[$i] != NULL){
$restricted_str .= '|'.$submenu[$menu_item[2]][$i][2];
unset($submenu[$menu_item[2]][$i]);
}
}
}
unset($menu[key($menu)]);
}
}
$result = preg_match('/(.*?)\/wp-admin\/?('.$restricted_str.')??(('.$restricted_str.'){1})(.*?)/',$_SERVER['REQUEST_URI']);
if ($result != 0 && $result != FALSE){
wp_redirect(get_option('siteurl') . '/wp-admin/' . $valid_page);
exit(0);
}
}
add_action('admin_menu', 'remove_menus');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment