Skip to content

Instantly share code, notes, and snippets.

Last active August 11, 2022 05:21
What would you like to do?
Programmatically adding a Page item to a Wordpress nav menu
* Adds Page to a WordPress navmenu
* @param [int] $page_id The ID of the page you want to add
* @param [str] $page_title Title of menu item
* @param [int] $menu_id NavMenu ID
* @param [int] $parent (Optional) Menu item Parent ID
function add_page_to_menu($page_id, $page_title, $menu_id, $parent = 0){
wp_update_nav_menu_item($menu_id, 0,
array( 'menu-item-title' => $page_title,
'menu-item-object' => 'page',
'menu-item-object-id' => $page_id,
'menu-item-type' => 'post_type',
'menu-item-status' => 'publish',
'menu-item-parent-id' => $parent));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment