Make WordPress Core

Opened 10 years ago

Last modified 5 years ago

#29728 new feature request

Could be very useful 'wp_remove_nav_menu_item'

Reported by: jonathanbesomi's profile JonathanBesomi Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Menus Keywords: has-patch needs-testing
Focuses: administration Cc:

Description

Hello there,

I was developing a plugin that need to manage dynamically wordpress navigation menu.
During develop I needed a function that remove a specific item by item 'id' or 'title'

wp_remove_nav_menu_item( $menu_id, $item_id or $item_title ).

I googled for this but I don't have found a good solutions that work with API.

I think that Wordpress could be better if you had this function in the next wp update.

I hope this help to make Wordpress a better services.

Thanks for developing this extraordinary CMS.

Best regards
Jonathan

Attachments (1)

wp_delete_nav_menu_item.patch (2.9 KB) - added by tyxla 10 years ago.
Implementation of wp_delete_nav_menu_item() and its usage in the core, as described in my last reply.

Download all attachments as: .zip

Change History (7)

#1 @joedolson
10 years ago

  • Focuses accessibility removed

#2 @johnbillion
10 years ago

  • Keywords needs-patch added
  • Version changed from trunk to 3.0

#3 @tyxla
10 years ago

Great idea.

Let me start with the fact that menu items are being stored as posts (of post type nav_menu_item) and menus are being stored as taxonomies (of taxonomy nav_menu). And menu items of a certain menu are simply menu item posts assigned to the menu term.

Therefore, deleting a menu item basically consists of deleting the nav_menu_item post by using wp_delete_post() function. So a separate function is not required, but will probably be more friendly for the developers.

Now, concerning the proposed solution by @JonathanBesomi: each menu item can be present only in one menu, so the $menu_id is not necessary - deleting the menu item post is sufficient. In addition, I believe deleting item by $item_title is not a good idea, as there can be multiple items with the same name, and this could unintentionally delete menu items that should not be deleted.

So in case this function is to be added to the core, I suggest that the function simply deletes the menu item by ID. Then this function can also be used to delete menu items in the core instead of using wp_delete_post() for that purpose.

I'll attach a patch in a couple of minutes.

@tyxla
10 years ago

Implementation of wp_delete_nav_menu_item() and its usage in the core, as described in my last reply.

#4 @tyxla
10 years ago

  • Keywords has-patch needs-testing dev-feedback added; needs-patch removed

#5 @tyxla
10 years ago

#31051 was marked as a duplicate.

#6 @tyxla
10 years ago

  • Keywords dev-feedback removed
Note: See TracTickets for help on using tickets.