Where to place PHP code that inserts (or doesn’t insert) menu item?

Use wp_get_nav_menu_items to iterate through the menu items, modifying the one that you want to in the loop. See the example code here for more information.