WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 15 months ago

#15777 closed enhancement (wontfix)

Add a filter to allow a menu item to be manually selected

Reported by: aaroncampbell Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Menus Keywords:
Focuses: Cc:

Description

I have several sites that want to be upgraded to use WP Menus, but they all do one thing that keeps them from switching. Basically, they have the need to be able to set up certain menu items as "pseudo-parents" for various pages. For example, on pon.harvard a tag page (like Negotiation Skills) should select "Glossary" in the menu. In this case "Glossary" is a page, so WP doesn't see any reason to select it when a user is on a tag page.

It would be nice to add a filter and possibly some extra code to make it possible to code in these kind of manual relationships.

Attachments (3)

15777.001.diff (1.5 KB) - added by aaroncampbell 3 years ago.
wordpress-nav-menu-force-active-inactive.patch (7.4 KB) - added by chalet16 3 years ago.
wordpress-nav-menu-force-active-inactive-update.patch (8.0 KB) - added by chalet16 3 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 aaroncampbell3 years ago

The patch attached works for me, but there are some limitations. The biggest is that since I added the filter toward the end of the if/elseif, it only works to select an item if it's not already been selected, but cannot force an item to NOT be selected. Maybe it should be moved before the current logic?

aaroncampbell3 years ago

comment:2 aaroncampbell3 years ago

  • Keywords has-patch needs-testing dev-feedback added

comment:3 aaroncampbell3 years ago

  • Cc aaroncampbell added
  • Keywords 3.2-early added
  • Milestone changed from Awaiting Review to Future Release

The patch attached was by a GCI student. We need to decide whether that's the way we want to do it, but this is a really important one for me.

comment:4 scribu21 months ago

  • Keywords needs-patch added; has-patch needs-testing removed

I don't think having both force_inactive and force_active makes sense.

Ideally, we would decide which is the active menu item and add a filter on it and only then display the menu.

Version 0, edited 21 months ago by scribu (next)

comment:5 kovshenin15 months ago

  • Cc kovshenin added

I think we should close this. Navigation menus are already filterable in many many places, including wp_nav_menu_items and wp_nav_menu_objects. Here's an example how one would always highlight Glossary in their menu:

function my_wp_nav_menu_objects( $menu_items, $args ) {
	foreach ( $menu_items as $item )
		if ( $item->post_name == 'glossary' )
			$item->classes[] = 'current-menu-item';

	return $menu_items;
}
add_filter( 'wp_nav_menu_objects', 'my_wp_nav_menu_objects', 10, 2 );

Add a condition to that (is_tag or is_tax) and you've solved your problem.

comment:6 SergeyBiryukov15 months ago

  • Keywords close reporter-feedback added; dev-feedback needs-patch removed

comment:7 aaroncampbell15 months ago

  • Keywords 3.2-early close reporter-feedback removed
  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from new to closed

So all the classes are added in _wp_menu_item_classes_by_context() which is called on line 198 of nav-menu-template.php. The filter you're talking about using is on line 206, which means that in that loop I'd need to unset all the previous classes ('current-menu-item', 'current-menu-ancestor', 'current-menu-parent', etc), find the item to select, pull it's parents, loop through them, and re-assign proper classes.

Basically, I'd need to create a slightly modified version of _wp_menu_item_classes_by_context() (which is about 200 lines) and use it.

Having said all that, I no longer work with the publishers that I used to, and haven't run across the need for this in probably 18 months. I think it's one of those things that felt common at the time and is actually edge case. Closing as wontfix.

Note: See TracTickets for help on using tickets.