Last active
August 11, 2022 05:21
-
-
Save eduwass/4dc3f4522f6a91d033d4 to your computer and use it in GitHub Desktop.
Programmatically adding a Page item to a Wordpress nav menu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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