Make WordPress Core

Opened 10 years ago

Closed 9 years ago

#30983 closed enhancement (fixed)

Missing Hook in wp_update_nav_menu_item function

Reported by: wpthemetrends's profile WPThemeTrends Owned by: drewapicture's profile DrewAPicture
Milestone: 4.4 Priority: normal
Severity: normal Version: 4.1
Component: Menus Keywords: has-patch
Focuses: Cc:

Description

Hi,

The function named "wp_update_nav_menu_item" contains an action at the end of the function ;

do_action( 'wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );

However, this only fired if menu item updated. But this function used for making new items as well. At the code line "394" to "400" it can return after making new item. However, this time the hook is not fired. Code reference ;

 if ( ! $update ) {
        $post['ID'] = 0;
        $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
        $menu_item_db_id = wp_insert_post( $post );
        if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) )
            return $menu_item_db_id;
    }

Code file ;

https://developer.wordpress.org/reference/functions/wp_update_nav_menu_item/

That code lines should be updated with that hook. Something like this ;

if ( ! $update ) {
        $post['ID'] = 0;
        $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
        $menu_item_db_id = wp_insert_post( $post );
        if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ){
            do_action( 'wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
            return $menu_item_db_id;
        }
    }

Attachments (3)

30983.diff (1.0 KB) - added by MikeHansenMe 10 years ago.
Add new hook for new menu items
30983.patch (812 bytes) - added by tyxla 10 years ago.
Updating the patch to call the new hook only if the menu item creation was successful.
30983.2.patch (855 bytes) - added by tyxla 10 years ago.
Updating the last patch to contain a @see wp_update_nav_menu_item() as a reference for the hook third parameter.

Download all attachments as: .zip

Change History (11)

@MikeHansenMe
10 years ago

Add new hook for new menu items

#1 @MikeHansenMe
10 years ago

I think based on the name of the hook being 'wp_update_nav_menu_item' we should add a new hook 'wp_add_nav_menu_item' for the situation you described. Another option is we could provide context by passing an additional variable but that seems less clear.

#2 @MikeHansenMe
10 years ago

  • Keywords has-patch added

#3 @tyxla
10 years ago

Agree with @MikeHansenMe on this one - a new hook here would be the best choice for clarity.

Also, I'm updating the patch, as the previous one will only call the hook if the new menu item creation has failed for some reason. Contrary to that behavior, the wp_add_nav_menu_item hook should be called only if the menu item creation was successful.

@tyxla
10 years ago

Updating the patch to call the new hook only if the menu item creation was successful.

@tyxla
10 years ago

Updating the last patch to contain a @see wp_update_nav_menu_item() as a reference for the hook third parameter.

#5 @DrewAPicture
9 years ago

I wonder if @westonruter knows if any transactional hooks were added for when menu items are added in the Menus in the Customizer work, because we'd likely need to cover both cases with such a hook.

#6 @westonruter
9 years ago

@DrewAPicture: If you look at WP_Customize_Nav_Menu_Item_Setting::update() you can see the last thing it does is call wp_update_nav_menu_item(), so this new action will get fired when saving in the Customizer as well.

#7 @DrewAPicture
9 years ago

  • Milestone changed from Awaiting Review to 4.4
  • Type changed from defect (bug) to enhancement

#8 @DrewAPicture
9 years ago

  • Owner set to DrewAPicture
  • Resolution set to fixed
  • Status changed from new to closed

In 34769:

Introduce the wp_add_nav_menu_item action, fired immediately after a new nav menu item has been added.

This hook can be leveraged both for the legacy menus interface and the menus integration in the Customizer, as both use the containing function, wp_update_nav_menu_item().

Props MikeHansenMe, tyxla.
Fixes #30983.

Note: See TracTickets for help on using tickets.