3

I try to remove a nav menu programmatically but I didn't find the way to do it. I knwo how to create it but I don't know how to delete it... I've got the menu name and I wan't to delete it like in wp admin panel but programmatically.

Or I want to delete all the menu item inside it.

4
  • Just delete it from admin. Commented Jul 20, 2014 at 16:29
  • programmatically
    – lolo
    Commented Jul 20, 2014 at 20:03
  • possible duplicate of Unregister Nav Menu with fallback? Commented Jul 20, 2014 at 20:33
  • I just try but it's not working in my case. In fact I want to completely delete the nav menu or item inside the nav menu then repopulate it... What wordpress do when you click on delete menu?
    – lolo
    Commented Jul 20, 2014 at 21:10

3 Answers 3

6

Finally the answer is simple:

$menu_name   = 'your menu';//name,id,slug
wp_delete_nav_menu($menu_name);
2
  • This answers the nav menu - But what about menu item? Commented Mar 11, 2021 at 10:44
  • In fact, this is a good start for deleting a menu and its items since WordPress handles this directly. Commented Dec 24, 2022 at 10:31
0

As the question was "Remove nav menu or menu item -programmatically-?"

Since WordPress 3.0, there is native dedicated function to do so: wp_delete_nav_menu(). It deletes the menu nav and all related menu items, no need to use wp_delete_post anymore.

So, depending on the context:

  • A. in pure code in php, use wp_delete_nav_menu();
  • B. in WP Cli, there is the wp menu delete command, which call wp_delete_nav_menu;

So to conclude, in both cases, all menu items and the menu will be deleted.

-1

This is an old question but I just found the solution for myself. In order to delete all (or some) menu items from a menu you could first get the menu items and then use wp_delete_post().

From: https://developer.wordpress.org/reference/functions/wp_delete_nav_menu/ (line 271)

$menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
if ( ! empty( $menu_objects ) ) {
    foreach ( $menu_objects as $item ) {
        wp_delete_post( $item );
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.