Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use add_menu_classes filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
December 20, 2022
5 minutes read

Hey there! If you’re diving into WordPress development, you might find yourself needing to tweak the administration menu items. That’s where the add_menu_classes filter comes in handy. This filter lets you add classes to top-level items in the admin menu. Let’s get into the nitty-gritty of how you can use this hook effectively.

add_menu_classes filter

Filters the administration menu array with classes added for top-level items.

To use the add_menu_classes filter, you first have to register it using add_filter. You can write this code into the functions.php of your activated theme or in a custom WordPress Plugin. Personally, I always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the live examples below, we’ve defined a function weplugins_modify_add_menu_classes_defaults which takes one parameter and is registered using add_filter. The first parameter, add_menu_classes, is the name of the hook. The second parameter, weplugins_modify_add_menu_classes_defaults, is the name of the function to be called. The third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed into the registered function.

Sometimes, you have to remove a registered hook, so you can use remove_filter to remove the add_menu_classes filter.

Parameters

    Below is the one parameter required to use this hook.

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

Live Example 1: Basic Usage

Here’s a basic example of how you can use this hook.

    function weplugins_modify_add_menu_classes_defaults($menu) { 
        // Update the $menu variable according to your website requirements and return this variable.
        // You can modify the $menu variable conditionally too if you want.
        return $menu; 
    }
    // Add the filter
    add_filter("add_menu_classes", "weplugins_modify_add_menu_classes_defaults", 10, 1);
    

Live Example 2: Conditional Menu Modification

In this example, we’re conditionally adding a class to the menu items based on certain conditions.

    function weplugins_modify_add_menu_classes_defaults($menu) { 
        foreach ($menu as &$item) {
            if ($item['id'] === 'menu-posts') {
                $item['classes'][] = 'custom-class';
            }
        }
        return $menu; 
    }
    // Add the filter
    add_filter("add_menu_classes", "weplugins_modify_add_menu_classes_defaults", 10, 1);
    

Live Example 3: Removing the Hook

To remove a hook callback, use the example below.

    // Remove the filter
    remove_filter("add_menu_classes", "weplugins_modify_add_menu_classes_defaults", 10, 1);
    

Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.

Access Premium WordPress Plugins

Contact Us

If you’re having any trouble using this hook, please contact our team and we’d be happy to assist you.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.