WordPress.org

Make WordPress Core

Opened 3 weeks ago

Last modified 3 weeks ago

#40359 new enhancement

allow arbitrary HTML attributes on <a> produced by Walker_Page

Reported by: pbiron Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch
Focuses: accessibility Cc:

Description

Walker_Nav_Menu currently allows arbitrary HTML attributes to be added to the <a> tags it generates via the nav_menu_link_attributes filter.

It would be useful to do the same in Walker_Page (i.e., add a page_menu_link_attributes filter).

My immediate need is to add "aria-current='page'" to the <a> tag for the current page in the output generated by wp_page_menu(). But other potential uses would be to add "target='_blank'", etc.

My current workaround is to subclass Walker_Page, duplicating Walker_Page::start_el() except for adding "aria-current='page'" when $page->ID === $current_page. But that is far from ideal...as I will have to track changes to Walker_Page::start_el() in future released and update my subclass appropriately.

There may be other places across core where a similar filter would also be useful (e.g., Walker_Category) and I'd be willing to look for those other places if my proposed addition to Walker_Page is accepted as a good idea.

Attachments (1)

page_menu_link_attributes.diff (1.6 KB) - added by pbiron 3 weeks ago.
patch implementing suggestion

Download all attachments as: .zip

Change History (3)

@pbiron
3 weeks ago

patch implementing suggestion

#1 @pbiron
3 weeks ago

  • Keywords has-patch added

#2 @pbiron
3 weeks ago

And here is a sample of a func I'd hook into the filter to satisfy my immediate need:

add_filter ('page_menu_link_attributes', 'add_aria_current', 10, 5) ;

function
add_aria_current ($attrs = array (), $page, $depth, $args, $current_page)
{
	if ($page->ID === $current_page) {
		$attrs['aria-current'] = 'page' ;
		}
//	if (my_is_external_link_func (get_permalink ($page->ID))) {
//		$attrs['target'] = '_blank' ;
//		}

	return ($attrs) ;
}

which, as you can see, would be much cleaner (and more future-proof) than my current workaround of subclassing Walker_Page.

Note: See TracTickets for help on using tickets.