WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#13371 closed defect (bug) (fixed)

Nav menu items should be filterable, regardless of context

Reported by: nathanrice Owned by: nacin
Milestone: 3.0 Priority: normal
Severity: normal Version: 3.0
Component: Menus Keywords: menus, nav menus, has-patch
Focuses: Cc:

Description

It's kind of weird that in order for a plugin/theme to filter the nav menu items, the $args->context needs to be 'frontend'.

For example, since the $args object is passed through the wp_nav_menu_items filter, the user-defined filter can do the contextual checks. For instance ...

add_filter('wp_nav_menu_items', 'my_nav_menu_items_filter', 10, 2);
function my_nav_menu_items_filter($items, $args) {
    if( $args->context != 'frontend' )
        return $items;

    // proceed with the filter
}

Plus, I can see plenty of reasons why a plugin/theme might want to filter the items regardless of context.

Either way, it should be up to the plugin/theme author, shouldn't it?

I've attached a patch.

Attachments (1)

nav-menu-template.php.diff (800 bytes) - added by nathanrice 4 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 follow-up: nacin4 years ago

  • Owner set to nacin
  • Status changed from new to accepted

This is a bit of a rabbit hole. OTOH, since we're already giving them access via the wp_nav_menu filter, I'd rather not tie plugin authors' hands and force them to regex through HTML to do that they need.

comment:2 in reply to: ↑ 1 nathanrice4 years ago

Replying to nacin:

This is a bit of a rabbit hole. OTOH, since we're already giving them access via the wp_nav_menu filter, I'd rather not tie plugin authors' hands and force them to regex through HTML to do that they need.

My thoughts exactly. Seems like the only way to do it, if the context isn't 'frontend'.

comment:3 follow-up: nacin4 years ago

  • Resolution set to fixed
  • Status changed from accepted to closed

This didn't make it through to Trac: [14592]

comment:4 in reply to: ↑ 3 nathanrice4 years ago

Replying to nacin:

This didn't make it through to Trac: [14592]

Cool, thanks!

Note: See TracTickets for help on using tickets.