Make WordPress Core

Opened 6 months ago

Closed 6 months ago

#50208 closed defect (bug) (duplicate)

wp_update_nav_menu hook is used twice but with different number of arguments

Reported by: icreative5 Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.4.1
Component: Menus Keywords: has-patch
Focuses: Cc:


wp_update_nav_menu action is used twice as mentioned here (https://developer.wordpress.org/reference/hooks/wp_update_nav_menu/)

Usage 1: wp-admin/includes/nav-menu.php: wp_nav_menu_update_menu_items()

        /** This action is documented in wp-includes/nav-menu.php */
        do_action( 'wp_update_nav_menu', $nav_menu_selected_id );

Usage 2: wp-includes/nav-menu.php: wp_update_nav_menu_object()

         * Fires after a navigation menu has been successfully updated.
         * @since 3.0.0
         * @param int   $menu_id   ID of the updated menu.
         * @param array $menu_data An array of menu data.
        do_action( 'wp_update_nav_menu', $menu_id, $menu_data );

This hook passes a single arguments when used in wp-admin/includes/nav-menu.php, whereas it passes two arguments in wp-includes/nav-menu.php. Now when I hook a function to this action with 2 arguments it is throwing a fatal error.

Example Code:

add_action( 'wp_update_nav_menu', array( $this, '_after_nav_menu_update' ), 10, 2 );
public function _after_nav_menu_update( $menu_id, $menu_data )
    { ...}

Error Produced:

Uncaught Error: Too few arguments to function BOB_Admin_Nav_Menu::_after_nav_menu_update(), 1 passed in /Users/stalky/flywheel/foodhublatest/app/public/wp-includes/class-wp-hook.php on line 289 and exactly 2 expected in /Users/stalky/flywheel/foodhublatest/app/public/wp-content/themes/foodhub/inc/modules/megamenu/class-bob-admin-nav-menu.php on line 135

Fix: If we modify the action in wp-admin/includes/nav-menu.php to the below it fixes the issue.

/** This action is documented in wp-includes/nav-menu.php */
        do_action( 'wp_update_nav_menu', $nav_menu_selected_id, array( "menu-name" => $nav_menu_selected_title ) );

Change History (2)

This ticket was mentioned in Slack in #core by icreative5. View the logs.

6 months ago

#2 @SergeyBiryukov
6 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi there, welcome to WordPress Trac!

Thanks for the report, we're already tracking this issue in #38057.

Note: See TracTickets for help on using tickets.