Add A Menu Item To A WP_Nav_Menu Navigation via PHP Function
You can use wp_update_nav_menu_item() to programmatically add a new menu item to the database. Since this change persists, you shouldn’t execute this functionality on every page – it’s best used as a one-off (though in that case it might be easier and more efficient to use WP CLI as suggested by WebElaine in the comments), … Read more