Add a Custom Class to Admin Menus

I am looking for a way to add some custom classes to the admin menus using PHP.

For example, here are the li tag for Posts and Pages:

<li id="menu-posts" class="wp-has-submenu wp-has-current-submenu wp-menu-open open-if-no-js menu-top menu-icon-post menu-top-first">
<li id="menu-pages" class="wp-has-submenu wp-not-current-submenu menu-top menu-icon-page">

In the Posts li tag I would like to add a custom class (e.g. custom_one) so that it would look like this:

<li id="menu-posts" class="custom_one wp-has-submenu wp-has-current-submenu wp-menu-open open-if-no-js menu-top menu-icon-post menu-top-first">

But for the Pages li tag I would like to add a different custom class (e.g., custom_two) so that it would look like this:

<li id="menu-pages" class="custom_two wp-has-submenu wp-not-current-submenu menu-top menu-icon-page">

Any idea how to do this vis-a-vis the functions.php file?

Thanks,

Moshe

1 Answer
1

The following does the job:

add_action( 'admin_init','wpse_60168_custom_menu_class' );

function wpse_60168_custom_menu_class() 
{
    global $menu;

    foreach( $menu as $key => $value )
    {
        if( 'Posts' == $value[0] )
            $menu[$key][4] .= " custom-class-1";

        if( 'Pages' == $value[0] )
            $menu[$key][4] .= " custom-class-2";
    }
}

And if you want to inspect what the $menu contains, use this:

add_action( 'admin_init','wpse_60168_var_dump_and_die' );

function wpse_60168_var_dump_and_die() 
{
    global $menu;   
    echo '<pre>' . print_r( $menu, true ) . '</pre>';
    wp_die();
}

Leave a Comment