Make WordPress Core

Opened 13 years ago

Closed 11 years ago

#21652 closed defect (bug) (fixed)

wp_update_nav_menu_item returns false instead of WP_Error for non-existent menu items

Reported by: mltsy's profile mltsy Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 3.9 Priority: normal
Severity: normal Version: 3.4.1
Component: Menus Keywords: has-patch commit
Focuses: Cc:

Description

Use case:
Calling wp_update_nav_menu_items with a non-existent menu_item_id as the first parameter

Expected return value: WP_Error object (as per the method's documentation)

Actual return value: Boolean false

Fix: wp-includes/nav-menu.php#L279 - This line should check for a false value first, and if $menu == false, set it equal to a new WP_Error object - then check is_wp_error($menu)

Attachments (2)

21652.patch (601 bytes) - added by bootsz 13 years ago.
Assigns new WP_Error to $menu if $menu is false, and then returns the error.
21652.2.patch (567 bytes) - added by ocean90 11 years ago.

Download all attachments as: .zip

Change History (7)

#1 @mltsy
13 years ago

  • Version set to 3.4.1

Correction: the first parameter is a menu_id, not a menu_item_id. So this happens when you provide a non-existent menu_id.

The main reason this bug is important to fix is for the error message the WP_Error would provide, which should be something like "The specified menu ID does not exist in this site".

Without that, I had no idea why the method was failing, and had to go looking through the source code :)

@bootsz
13 years ago

Assigns new WP_Error to $menu if $menu is false, and then returns the error.

#2 @bootsz
13 years ago

  • Cc sbutze@… added
  • Keywords has-patch added

#3 @ocean90
11 years ago

  • Keywords commit added
  • Milestone changed from Awaiting Review to 3.9

Hello mltsy and bootsz, thanks for the report and the patch.

I agree, the error handling can be improved here. Since we already returning WP_Error objects we easily can add one more, so the change is backward compatible.

We're doing similar checks on other places, like wp_insert_term() or is_object_in_term(), where we just declare the input as invalid. In 21652.2.patch I have used a similar phrase.

Last edited 11 years ago by ocean90 (previous) (diff)

#4 @ocean90
11 years ago

  • Summary changed from wp_update_nav_menu_items returns false instead of WP_Error for non-existent menu items to wp_update_nav_menu_item returns false instead of WP_Error for non-existent menu items

@ocean90
11 years ago

#5 @SergeyBiryukov
11 years ago

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

In 27251:

Consistently return a WP_Error object in case of an error in wp_update_nav_menu_item().

props bootsz, ocean90.
fixes #21652.

Note: See TracTickets for help on using tickets.