Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer

up vote 1 down vote accepted

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();
}
share|improve this answer
That's exactly what I needed - thanks. With that said, one more related question - what if I wanted to assign Posts and Pages the same custom class - could I do that with one if statement? If so, how? Thanks. – user15927 Jul 30 '12 at 19:10
Actually - I figured out my last question - here is what I did: code if( 'Posts' == $value[0] || 'Pages' == $value[0] ) $menu[$key][4] .= " new-custom-class"; } – user15927 Jul 30 '12 at 19:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.