WP_Navigation_Fallback::create_classic_menu_fallback()private staticWP 6.3.0

Creates a Navigation Menu post from a Classic Menu.

Method of the class: WP_Navigation_Fallback{}

No Hooks.

Return

Int|WP_Error. The post ID of the default fallback menu or a WP_Error object.

Usage

$result = WP_Navigation_Fallback::create_classic_menu_fallback();

Changelog

Since 6.3.0 Introduced.

WP_Navigation_Fallback::create_classic_menu_fallback() code WP 6.6.2

private static function create_classic_menu_fallback() {
	// See if we have a classic menu.
	$classic_nav_menu = static::get_fallback_classic_menu();

	if ( ! $classic_nav_menu ) {
		return new WP_Error( 'no_classic_menus', __( 'No Classic Menus found.' ) );
	}

	// If there is a classic menu then convert it to blocks.
	$classic_nav_menu_blocks = WP_Classic_To_Block_Menu_Converter::convert( $classic_nav_menu );

	if ( is_wp_error( $classic_nav_menu_blocks ) ) {
		return $classic_nav_menu_blocks;
	}

	if ( empty( $classic_nav_menu_blocks ) ) {
		return new WP_Error( 'cannot_convert_classic_menu', __( 'Unable to convert Classic Menu to blocks.' ) );
	}

	// Create a new navigation menu from the classic menu.
	$classic_menu_fallback = wp_insert_post(
		array(
			'post_content' => $classic_nav_menu_blocks,
			'post_title'   => $classic_nav_menu->name,
			'post_name'    => $classic_nav_menu->slug,
			'post_status'  => 'publish',
			'post_type'    => 'wp_navigation',
		),
		true // So that we can check whether the result is an error.
	);

	return $classic_menu_fallback;
}