Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
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

2 Answers 2

up vote 8 down vote accepted

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!

share|improve this answer
    
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
    
Thank you so much for your help! –  INT Jul 21 '12 at 23:32

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.
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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