You should utilize wp_update_nav_menu_item()
to programmatically add a brand new menu merchandise to the database. Since this variation persists, you should not execute this performance on each web page – it is best used as a one-off (although in that case it could be simpler and extra environment friendly to make use of WP CLI as instructed by WebElaine within the feedback), or by way of some dashboard interplay if it is one thing you propose to make use of repeatedly.
/**
* Insert a brand new navigation menu merchandise for a web page into the database.
*
* @hyperlink https://wordpress.stackexchange.com/questions/408618
*
* @param int $menu_id The ID of the menu to insert the brand new merchandise into.
* @param int|WP_Post|null $web page A reference to the web page for which to create a brand new menu merchandise.
* @param string $title The title of the nav merchandise. Defaults to the web page title.
* @param int $mum or dad The ID of the mum or dad nav menu merchandise, if relevant.
* @param int $place The index throughout the menu/mum or dad for the brand new merchandise. Defaults to 0 to append.
* @return int|WP_Error The ID of the brand new menu merchandise on success, or a WP_Error object on failure.
**/
perform wpse408618_insert_nav_menu_page_item(
$menu_id,
$web page,
$title = null,
$mum or dad = 0,
$place = 0
)
$new_item_id = wpse408618_insert_nav_menu_page_item( 158, 15891 );
if( is_wp_error( $new_item_id ) )
echo 'Web page nav menu merchandise creation failed: "' . $new_item_id->get_error_message() . '"';
else
echo 'Web page nav menu merchandise creation succeeded! Merchandise inserted with ID ' . $new_item_id;
You possibly can check with the supply of the wp_update_nav_menu_item()
perform for a extra full checklist of doable nav menu merchandise object attributes.
It is also doable to make use of a hook such because the wp_get_nav_menu_items
filter so as to add non-persistent gadgets on the fly, if it makes extra sense to your software.