Wednesday, June 19, 2013

WordPress: remove admin menu items

There are several well-written WordPress plugins that allow removing and re-ordering admin menu items. However, if you want something quick and simple for your site, the code snippet below may be useful. It removes all menus, except for Dashboard and WooCommerce "Products".

Feel free to modify is as necessary.

► Tip: you can print_r($menu) to see what to keep.


add_action('admin_menu', function () {
 if (current_user_can('administrator')) {
  return;
 }

 /**
  * Keep only specific menu items and remove all others
  */
 global $menu;
 $hMenu = $menu;
 foreach ($hMenu as $nMenuIndex => $hMenuItem) {
  if (in_array($hMenuItem[2], array(
       'index.php',
       'edit.php?post_type=product',
      ))
  ) {
   continue;
  }
  unset($menu[$nMenuIndex]);
 }
}