[wp-trac] [WordPress Trac] #50208: wp_update_nav_menu hook is used twice but with different number of arguments

WordPress Trac noreply at wordpress.org
Tue May 19 10:17:42 UTC 2020


#50208: wp_update_nav_menu hook is used twice but with different number of
arguments
--------------------------+-----------------------------
 Reporter:  icreative5    |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Menus         |    Version:  5.4.1
 Severity:  normal        |   Keywords:  has-patch
  Focuses:                |
--------------------------+-----------------------------
 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()


 {{{#!php
 <?php
         /** 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()

 {{{#!php
 <?php
 /**
          * 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:
 {{{#!php
 <?php
 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.

 {{{#!php
 <?php
 /** 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 ) );
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/50208>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list