Opened 10 years ago
Closed 9 years ago
#30983 closed enhancement (fixed)
Missing Hook in wp_update_nav_menu_item function
Reported by: | WPThemeTrends | Owned by: | 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)
Change History (11)
#1
@
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.
#3
@
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.
@
10 years ago
Updating the patch to call the new hook only if the menu item creation was successful.
@
10 years ago
Updating the last patch to contain a @see wp_update_nav_menu_item()
as a reference for the hook third parameter.
#4
@
9 years ago
30983.3.diff still applies
#5
@
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
@
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.
Add new hook for new menu items