How To Create a Pre-filled Custom Menu

Snippets by IsItWP

Are you looking for a way to create a pre-filled custom menu? This snippet will create a menu, set the theme location to show the new menu, and add a page to the menu.


All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin. Then optionally edit the theme location that is created on line 5 and edit line 19 to match, edit the link title on line 14, and edit the menu name on line 8. Also, optionally edit the page id, which is the page added to the menu, on line 10.

add_action( 'init', 'add_menus' );
function add_menus() {
			'main_nav' => 'The main menu',
	$new_menu_id = wp_create_nav_menu('Main Menu');
	$page_args = array(
		'menu-item-object-id' => 1,
		'menu-item-object' => 'page',
		'menu-item-parent-id' => 0,
		'menu-item-type' => 'post_type',
		'menu-item-title' => 'THIS WILL BE THE LINK TITLE',
		'menu-item-status' => 'publish',
	if ( $new_menu_id > 0 ) {
		// set our new MENU up at our theme's nav menu location
		set_theme_mod( 'nav_menu_locations' , array( 'main_nav' => $new_menu_id ) );
		// add a menu item to that new menu
		wp_update_nav_menu_item( $new_menu_id , 0, $page_args );

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 12 Best WordPress Plugins for Affiliate Marketers and how to secure your WordPress forms.

Comments   Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!