Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How to get active menu class in wordpress, I want to get it in one plugin to change menu style.

Just to clear, I do not want active menu element class.

<ul id="menu-scroll" class="nav-menu">
<li id="menu-item-1461" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1461"><a href="#home">Home</a></li>
<li id="menu-item-1462" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1462"><a href="#about">About</a></li>
<li id="menu-item-1463" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1463"><a href="#services">Services</a></li>
<li id="menu-item-1466" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1466"><a href="#team">Team</a></li>
<li id="menu-item-1468" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1468"><a href="#blog">Blog</a></li>
<li id="menu-item-1467" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1467"><a href="#contact">Contact</a></li>
</ul>

In this I want to get "nav-menu" or i want to set new class like

<ul id="menu-scroll" class="my-nav-menu">

But not using themes file, I want to work it for all theme which is active.

Thanks In Advance, Ajay

share|improve this question
    
You can change it using js, just remove old class and replace it with your new class. –  Vinod Dalvi Sep 5 '13 at 12:41
    
But how I know old class for different theme ? –  Ajay Patel Sep 5 '13 at 12:45
    
There are standard class names for it codex.wordpress.org/Function_Reference/… but themes can modify it. –  Vinod Dalvi Sep 5 '13 at 12:47
    
That is the question, I want active theme active menu get, set class/id. –  Ajay Patel Sep 5 '13 at 12:50

2 Answers 2

This is relatively easily done with a filter.

function alter_menu_class_wpse_113022($args) {
  // var_dump($args); // debug
  $args['menu_class'] = 'my-nav-menu';
  return $args;
}
add_filter('wp_nav_menu_args','alter_menu_class_wpse_113022');

That will make this line:

<ul id="menu-scroll" class="nav-menu">

Look like this one:

<ul id="menu-scroll" class="my-nav-menu">

But that could cause themes to break horribly. If a theme has used that class to apply styles to the menu, which would be a reasonable thing to do, you've just broken all of that styling. The effected menus will look terrible and may not even function. This is a very, very bad idea. Please don't do it.

share|improve this answer
    
This code changes class of menu wrapper and not the class of ul tag. –  Vinod Dalvi Sep 5 '13 at 13:11
    
What If I want to set new class ? –  Ajay Patel Sep 5 '13 at 13:14
    
No. It doesn't. It changes the class of the ul tag. –  s_ha_dum Sep 5 '13 at 13:20
    
@s_ha_dum I have checked it on twentythirteen theme and it's not adding class to ul instead it's adding class to ul wrapper div. –  Vinod Dalvi Sep 5 '13 at 13:22
    
@VinodDalvi : I am looking at twentythirteen and it is inf fact changing the class on the ul and not the class on the surrounding div. I have no idea what you are looking at but I can alter that ul class very consistently by toggling that filter. –  s_ha_dum Sep 5 '13 at 13:29

This is the following safer way to add class to nav menu wrapper as it will not overwrite existing class.

function append_new_menu_class($args) {
  $args['menu_class'] = $args['menu_class'].' my-nav-menu';
  return $args;
}
add_filter('wp_nav_menu_args','append_new_menu_class');

The following code adds custom class to nav menu ul tag

function add_menuclass( $ulclass ) {
  return preg_replace( '/<ul>/', '<ul class="custom-class">', $ulclass, 1 );
}
add_filter( 'wp_page_menu', 'add_menuclass' );
share|improve this answer
    
I have added it on plugin file even in theme file but not adding, :l ? –  Ajay Patel Sep 5 '13 at 13:23
    
Yes, but that is not what the OP is asking for and it is a trivial edit to my answer anyway. –  s_ha_dum Sep 5 '13 at 13:26
    
@s_ha_dum I have added code for adding class to nav menu wrapper and also to nav menu ul. –  Vinod Dalvi Sep 5 '13 at 13:46

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.