4

As it is now, when a page that is in the nav menu is trashed, it still stays in the menu until manually remove via the nav menu editor. Is it possible to have trashed pages removed automatically?

Thanks

2
  • Are you talking about when a page gets trashed? Because menu items are removed when you delete a page/post. Jul 21, 2012 at 12:53
  • @TheDeadMedic Yes, you are correct. Sorry about that.
    – INT
    Jul 21, 2012 at 13:32

2 Answers 2

10

Just hook the default delete_post handler for menus onto the trash action too:

add_action( 'wp_trash_post', '_wp_delete_post_menu_item' );

How simple is that!

4
  • Oh, that works like a charm! Thanks so much. Is there some easy way to have the page be brought back to the nav menu if the page is untrashed?
    – INT
    Jul 21, 2012 at 15:42
  • If you have "auto add new pages" enabled for the menu, it'll work on trashed ones too. But it won't put it back where it was. Jul 21, 2012 at 17:31
  • Right. No other way? I deliberately disabled that because a new top level menu item would mess with my design.
    – INT
    Jul 21, 2012 at 22:11
  • 1
    Of course there's a way - check out _wp_auto_add_pages_to_menu(). That should give you ground to create a function that auto-adds trash-to-publish posts, then hook it onto transition_post_status :) Jul 21, 2012 at 23:20
3

You'll need a plugin which:

  1. Adds a method to the deleted_post action (taking the page's id)

  2. Filters: the action is used for posts etc. as well.

  3. Updates the menu by

    • wp_get_nav_menu to get its items
    • remove the item representing the page
    • update the menu with wp_update_nav_menu_object.
2
  • Richard, do you mean: " You'll need to build a plugin which: " ? +1 for the blue print :)
    – brasofilo
    Jul 21, 2012 at 20:27
  • @brasofilo Yes, if you cannot find one that already exists.
    – Richard
    Jul 21, 2012 at 20:32

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.