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. Jul 20, 2014 at 16:29
  • programmatically
    – lolo
    Jul 20, 2014 at 20:03
  • possible duplicate of Unregister Nav Menu with fallback? 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
    Jul 20, 2014 at 21:10

2 Answers 2

6

Finally the answer is simple:

$menu_name   = 'your menu';//name,id,slug
wp_delete_nav_menu($menu_name);
1
  • This answers the nav menu - But what about menu item? Mar 11, 2021 at 10:44
0

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, privacy policy and cookie policy

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