WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 3 years ago

Last modified 3 years ago

#16738 closed enhancement (fixed)

Add filter for attributes on menu item links

Reported by: simonwheatley Owned by: SergeyBiryukov
Milestone: 3.6 Priority: normal
Severity: normal Version:
Component: Menus Keywords: has-patch commit needs-codex
Focuses: Cc:

Description

I find myself needing to change the attributes of the link element of each menu item to add some classes. Currently I can do this by extending and substituting Walker_Nav_Menu, but that the start_el method I replace is fairly long and I'm nervous of changes to core breaking my kit in the future. Much better to be able to hook a filter, IMO.

Attachments (4)

add nav_menu_a_attributes filter.patch (1.5 KB) - added by simonwheatley 5 years ago.
I propose a new nav_menu_a_attributes filter in the start_el method of Walker_Nav_Menu, as per the attached patch.
16738.diff (1.5 KB) - added by DrewAPicture 3 years ago.
Keeping relatively as succinct as before.
16738.2.diff (1.5 KB) - added by DrewAPicture 3 years ago.
16738.3.diff (1.5 KB) - added by SergeyBiryukov 3 years ago.

Download all attachments as: .zip

Change History (17)

@simonwheatley
5 years ago

I propose a new nav_menu_a_attributes filter in the start_el method of Walker_Nav_Menu, as per the attached patch.

#1 @simonwheatley
5 years ago

  • Keywords has-patch added

#2 @simonwheatley
5 years ago

In the patch I've also changed the way the attributes are built up within start_el so they work with a named array, as it is easier for plugin authors to then parse the attributes as an array than if they are passed a long string of HTML attributes. Hope that makes sense.

#3 @scribu
5 years ago

Looks good to me.

@DrewAPicture
3 years ago

Keeping relatively as succinct as before.

#4 @DrewAPicture
3 years ago

  • Cc xoodrew@… added

I like this. Would be nice to get this in.

16738.diff refreshes @simonwheatley's patch and makes an effort to keep the format more succinct like it was originally.

#5 @SergeyBiryukov
3 years ago

  • Milestone changed from Awaiting Review to 3.6

#6 @SergeyBiryukov
3 years ago

Perhaps nav_menu_link_attributes is a better name, for consistency with *_posts_link_attributes and *_comments_link_attributes.

@DrewAPicture
3 years ago

#7 @DrewAPicture
3 years ago

Replying to SergeyBiryukov:

Perhaps nav_menu_link_attributes is a better name, for consistency with *_posts_link_attributes and *_comments_link_attributes.

+1 for consistency in naming :) Updated the patch.

#8 @SergeyBiryukov
3 years ago

If a plugin adds $atts['class'] = 'class="my-menu-link"', adding a second class may be a non-trivial task.

I guess it would be easier to modify the attributes if the array values were the actual attribute values rather than key-value pairs: 16738.3.diff.

#9 @DrewAPicture
3 years ago

16738.3.diff works for me.

#10 @SergeyBiryukov
3 years ago

  • Keywords commit added; dev-feedback removed

#11 @SergeyBiryukov
3 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 23565:

Add a filter for attributes on menu item links. props simonwheatley, DrewAPicture, SergeyBiryukov, nacin. fixes #16738.

#12 @DrewAPicture
3 years ago

  • Keywords 3.6-codex added

#13 @DrewAPicture
3 years ago

  • Keywords needs-codex added; 3.6-codex removed
Note: See TracTickets for help on using tickets.