How to create pages and a navigation menu automatically?

Sometimes, to quickly create a prototype, I install a WordPress with WP-CLI, an homemade template, and create the navigation (using this script, for example).

To go a step further, below is a script that allows you to create pages (with nesting on up to 2 levels) and the corresponding navigation menu.

Add this script to your functions.php file:

<?php
/*
*	List of post or page to create dynamically
*
* Structure must be in this format:
*	order_id => ["title","template.php" (only if post_type = page), "post type"]
*
*	1st level elements must be hundreds, sub-levels must be in the hundreds (e.g. 110, 120, ...)
*
*/
$menu_name = 'Menu Principal';
$items = [
	100 => ["Accueil"],
	200 => ["Chambres"],
		210 => ["Supérieure Nord"],
		220 => ["Supérieure Sud"],
		230 => ["Supérieure Deluxe"],
		240 => ["Junior Suite Nord"],
		250 => ["Junior Suite Sud"],
		260 => ["Suites"],
	300 => ["SPA"],
		310 => ["Le Wellness"],
		320 => ["Les Soins"],
		330 => ["Les Produits"],
		340 => ["Cures et Programmes"],	
	400 => ["Offres"],
	500 => ["Activités"],
	600 => ["Contact"],
];

add_action('after_setup_theme', 'wpster_create_pages_and_menu');
function wpster_create_pages_and_menu() {
	global $items, $menu_name;
	// create menu if not exists
	$menu_exists = wp_get_nav_menu_object($menu_name);
	if (!$menu_exists) $menu_id = wp_create_nav_menu($menu_name);
	else $menu_id = $menu_exists->term_id;

	foreach ($items as $key => $value) {
		if($key % 100 == 0):
			$parent = $key;
			$title = $items[$key][0];		
			$parent_slug = sanitize_title(trim($title));
			$parent_id = '';
			$parent_nav_id = '';
		else:
			$my_post = get_page_by_path($parent_slug, '', 'page');
			if(isset($my_post)) $parent_id = $my_post->ID;
			else $parent_id = '';
		endif;
		$menu_order = $key;
		$title = $items[$key][0];
		if(isset($items[$key][1])) $template = $items[$key][1];
		else $template = 'flex.php';
		if(isset($items[$key][2])) $type = $items[$key][2];
		else $type = 'page';
		$post_id = wpster_create_page("$title", $menu_order, "$type", "$template", $parent_id);
		$nav_id = wpster_add_page_to_navigation($menu_id, $post_id, "$title", $menu_order, "$type", $parent_nav_id);
		if($parent_id=='') $parent_nav_id = $nav_id;
	}    
} 
/**
 * Function to create a page if not exists
 *
 * @return post ID
 */
function wpster_create_page($title, $menu_order, $type, $template, $parent_id=NULL) {
	$slug = sanitize_title(trim($title));
	$new_page = array(
		'post_type'				=> $type,
		'post_title'			=> $title,
		'post_author'			=> get_current_user_id(),
		'post_name'     	=> $slug,
		'post_parent' 	  => $parent_id,
		'menu_order'			=> $menu_order,
		'page_template'		=> $template,
		'post_status'			=> 'publish',
		'comment_status'	=> 'close',
		'ping_status'			=> 'close',
	);
	$post = wpster_get_page_by_slug( $slug, OBJECT, $type);
	if (!$post) return wp_insert_post($new_page);
	else return $post->ID;
}
/**
 * Function to add page to navigation menu if not exists
 *
 * @return post ID
 */
function wpster_add_page_to_navigation($menu_id, $post_id, $title, $menu_order, $type, $parent_nav_id='') {
	if(cms_is_in_menu($menu_id, $post_id)==false):
		return wp_update_nav_menu_item($menu_id, 0, array(
			'menu-item-title' 		=> $title,
			'menu-item-object-id'	=> $post_id,
			'menu-item-object'		=> $type,
			'menu-item-status'		=> 'publish',
			'menu-item-type'			=> 'post_type',
			'menu-item-parent-id' => $parent_nav_id,
			'menu-item-position'  => $menu_order,		
		));
	endif;
}
/**
 * Function to get page_id by slug
 *
 * @return post ID
 */
function wpster_get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) { 
	global $wpdb; 
	$page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) ); 
	 if ( $page ) return get_post($page, $output); 
	return null;
}
/**
 * Function to check if a page is already in navigation menu
 *
 * @param $menu menu name, id, or slug
 * @param $object_id int post object id of page
 * @return bool true if object is in menu
 */
function cms_is_in_menu( $menu = null, $object_id = null ) {
	$menu_object = wp_get_nav_menu_items( esc_attr( $menu ) );
	if( ! $menu_object ) return false;
	$menu_items = wp_list_pluck( $menu_object, 'object_id' );
	if( !$object_id ) {
		global $post;
		$object_id = get_queried_object_id();
	}
	return in_array( (int) $object_id, $menu_items );	
}  
?>

Submit a Comment

Your email address will not be published. Required fields are marked *