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

  • Are you talking about when a page gets trashed? Because menu items are removed when you delete a page/post. – TheDeadMedic Jul 21 '12 at 12:53
  • @TheDeadMedic Yes, you are correct. Sorry about that. – INT Jul 21 '12 at 13:32
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!

  • 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 '12 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. – TheDeadMedic Jul 21 '12 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 '12 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 :) – TheDeadMedic Jul 21 '12 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.
  • Richard, do you mean: " You'll need to build a plugin which: " ? +1 for the blue print :) – brasofilo Jul 21 '12 at 20:27
  • @brasofilo Yes, if you cannot find one that already exists. – Richard Jul 21 '12 at 20:32

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.