Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#33891 closed defect (bug) (invalid)

apply_filters bug

Reported by: rgout's profile rgout Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.4
Component: General Keywords:
Focuses: Cc:

Description

Hello,

While I was trying to change some menu items with a filter I encountered a strange bug.

When I try to use the filter: wp_nav_menu_items it is supposed to send $items and $args as denoted by the documentation here: https://developer.wordpress.org/reference/hooks/wp_nav_menu_items/
I also checked the code just to be sure that $args should indeed be sent.

Upon trying to debug the issue I found out that $args is alive and well before applying the filter and also seems to be fine after the filter.
However, when you hook into the filter and use the params like this:

    function modify_nav_menu_items($items, $args) {

        // Do something here

        return $items;
    }

    add_filter('wp_nav_menu_items', 'modify_nav_menu_items');

At this point, $args is NULL
It seems to get lost somewhere in the process of the apply_filter() function.

I am running 4.3.1 since this morning but I have never tested this bug on an older installation.

Change History (4)

#1 @SergeyBiryukov
9 years ago

  • Keywords close added

Hi @rgout, welcome to Trac!

You have to specify the number of arguments in your add_filter() call:

add_filter( 'wp_nav_menu_items', 'modify_nav_menu_items', 10, 2 );

#2 follow-up: @rgout
9 years ago

  • Resolution set to invalid
  • Status changed from new to closed

Wow, I feel like such a fool right now.

Sure enough, with '10, 2' added it works fine!

#3 @SergeyBiryukov
9 years ago

  • Keywords close removed
  • Milestone Awaiting Review deleted

#4 in reply to: ↑ 2 @DrewAPicture
9 years ago

Replying to rgout:

Wow, I feel like such a fool right now.

Sure enough, with '10, 2' added it works fine!

We've all done it one time or another. Glad you got it working ;)

Note: See TracTickets for help on using tickets.