Using WordPress ‘add_menu_classes’ PHP filter

The add_menu_classes WordPress PHP filter allows you to modify the administration menu array by adding custom classes to the top-level items.

Usage

add_filter('add_menu_classes', 'my_custom_add_menu_classes');
function my_custom_add_menu_classes($menu) {
  // your custom code here
  return $menu;
}

Parameters

  • $menu (array): Associative array of administration menu items.

More information

See WordPress Developer Resources: add_menu_classes

Examples

Add a custom class to a specific menu item

Add the class ‘custom-class’ to the ‘Posts’ menu item.

add_filter('add_menu_classes', 'add_class_to_posts_menu');
function add_class_to_posts_menu($menu) {
  if (isset($menu['edit.php'])) {
    $menu['edit.php'][4] .= ' custom-class';
  }
  return $menu;
}

Add a custom class to all top-level menu items

Add the class ‘all-menu-items’ to every top-level menu item.

add_filter('add_menu_classes', 'add_class_to_all_menu_items');
function add_class_to_all_menu_items($menu) {
  foreach ($menu as $key => $value) {
    $menu[$key][4] .= ' all-menu-items';
  }
  return $menu;
}

Remove default classes from a specific menu item

Remove the default class ‘wp-not-current-submenu’ from the ‘Pages’ menu item.

add_filter('add_menu_classes', 'remove_class_from_pages_menu');
function remove_class_from_pages_menu($menu) {
  if (isset($menu['edit.php?post_type=page'])) {
    $menu['edit.php?post_type=page'][4] = str_replace('wp-not-current-submenu', '', $menu['edit.php?post_type=page'][4]);
  }
  return $menu;
}

Add an icon to a specific menu item

Add a custom icon to the ‘Media’ menu item using CSS class ‘my-custom-icon’.

add_filter('add_menu_classes', 'add_icon_to_media_menu');
function add_icon_to_media_menu($menu) {
  if (isset($menu['upload.php'])) {
    $menu['upload.php'][6] = 'my-custom-icon';
  }
  return $menu;
}

Change the order of menu items

Move the ‘Comments’ menu item to the top of the menu.

add_filter('add_menu_classes', 'change_menu_order');
function change_menu_order($menu) {
  if (isset($menu['edit-comments.php'])) {
    $comments_menu = $menu['edit-comments.php'];
    unset($menu['edit-comments.php']);
    array_unshift($menu, $comments_menu);
  }
  return $menu;
}