Skip to content

Instantly share code, notes, and snippets.

@eduwass
Last active August 11, 2022 05:21
Show Gist options
  • Save eduwass/4dc3f4522f6a91d033d4 to your computer and use it in GitHub Desktop.
Save eduwass/4dc3f4522f6a91d033d4 to your computer and use it in GitHub Desktop.
Programmatically adding a Page item to a Wordpress nav menu
<?php
/**
* 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