WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 14 months ago

Last modified 14 months 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 3 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 14 months ago.
Keeping relatively as succinct as before.
16738.2.diff (1.5 KB) - added by DrewAPicture 14 months ago.
16738.3.diff (1.5 KB) - added by SergeyBiryukov 14 months ago.

Download all attachments as: .zip

Change History (17)

simonwheatley3 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.

comment:1 simonwheatley3 years ago

  • Keywords has-patch added

comment:2 simonwheatley3 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.

comment:3 scribu3 years ago

Looks good to me.

DrewAPicture14 months ago

Keeping relatively as succinct as before.

comment:4 DrewAPicture14 months 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.

comment:5 SergeyBiryukov14 months ago

  • Milestone changed from Awaiting Review to 3.6

comment:6 SergeyBiryukov14 months ago

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

DrewAPicture14 months ago

comment:7 DrewAPicture14 months 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.

SergeyBiryukov14 months ago

comment:8 SergeyBiryukov14 months 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.

comment:9 DrewAPicture14 months ago

16738.3.diff works for me.

comment:10 SergeyBiryukov14 months ago

  • Keywords commit added; dev-feedback removed

comment:11 SergeyBiryukov14 months 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.

comment:12 DrewAPicture14 months ago

  • Keywords 3.6-codex added

comment:13 DrewAPicture14 months ago

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