Pre-fill a Custom Menu

We know how to Add Custom Menus Support to our theme, to allow a THEME LOCATION of “main_nav” (for example), but what if you also want to automate the next steps of creating a Menu itself, setting the Theme Location to show that new Menu, and adding a Page to that new Custom Menu ?

We’ll start with the Add Custom Menus Support code to register a Menu Location of “The main menu” / “main_nav”, and take it that next couple steps by creating an actual Menu called “Main Menu”, adding a link titled ‘THIS WILL BE THE LINK TITLE’ linking to page id #1, and telling WP to set our new “Main Menu” as THE menu for the “main_nav” theme location.

Code Snippet:

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 );

Posted in Admin area, Pure hacks