WordPress.org

Make WordPress Core

Opened 20 months ago

Closed 8 weeks 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 Owned by: 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 20 months ago.
Assigns new WP_Error to $menu if $menu is false, and then returns the error.
21652.2.patch (567 bytes) - added by ocean90 8 weeks ago.

Download all attachments as: .zip

Change History (7)

comment:1 mltsy20 months 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 :)

bootsz20 months ago

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

comment:2 bootsz20 months ago

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

comment:3 ocean908 weeks 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 8 weeks ago by ocean90 (previous) (diff)

comment:4 ocean908 weeks 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

ocean908 weeks ago

comment:5 SergeyBiryukov8 weeks 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.