[wp-hackers] nav_menu_link_attributes filter adds ending slash

Thomas Belknap dragonfly at dragonflyeye.net
Wed Dec 2 18:45:06 UTC 2015

I'm using the nav_menu_link_attributes filter to change the behavior of a
specific post type. The first thing I noticed was that, in contrast to what
the documentation says on this, $item refers to the *menu* item, not to the
object to which it is linked.

But beyond that, it seems that despite the fact that I'm replacing the
'href' attribute of the link, something somewhere is adding a trailing
slash, which is messing up what I'm doing. Here's the code I'm using:

function nav_menu_panel_item( $attrs, $item, $args ) {
if( stristr($attrs['href'], 'hn_panel') ) :
preg_match( '|([^/]+)/?$|', $attrs['href'], $target );
$attrs['href'] = '#' . $target[0];
$attrs['class'] = stristr( $attrs['class'], 'smoothscroll' ) ?
$attrs['class'] : $attrs['class'] . ' smoothscroll';
return $attrs;
add_filter( 'nav_menu_link_attributes', array( &$this,
'nav_menu_panel_item' ), 10, 3 );

As you can see, the point is to convert the "hn_panel" post type menu items
into on-page anchor navigation. But by adding the trailing slash, WP is
preventing this function from working.

Is there another filter that I'm missing? I've set the priority from 1 to
100 and back to 10, but that doesn't seem to help.


Tom Belknap
Owner/Editor, DragonFlyEye.Net
Freelance writer
Join me:

   - *Inbox:* http://dragonflyeye.net/subscribe
   - *Web*: http://dragonflyeye.net/
   - *Facebook*: https://www.facebook.com/DragonFlyEye.Net
   - *Twitter*: https://twitter.com/dragonflyeye
   - *Google*: https://plus.google.com/u/1/103251482414903117843/posts

More information about the wp-hackers mailing list