diff --git a/classes/Upgrades/NavMenus.php b/classes/Upgrades/NavMenus.php index bc7226d..c6c01f4 100644 --- a/classes/Upgrades/NavMenus.php +++ b/classes/Upgrades/NavMenus.php @@ -100,8 +100,9 @@ public function process( $item ) { switch_to_blog( $site_id ); - $locations = get_theme_mod( 'nav_menu_locations' ); - $menu_id = isset( $locations['primary'] ) ? (int) $locations['primary'] : 0; + $locations = get_theme_mod( 'nav_menu_locations' ); + $primary_nav_key = cboxol_get_theme_primary_nav_menu_location(); + $menu_id = isset( $locations[ $primary_nav_key ] ) ? (int) $locations[ $primary_nav_key ] : 0; if ( ! $menu_id ) { restore_current_blog(); diff --git a/includes/group-sites.php b/includes/group-sites.php index 3bb072d..9329db9 100644 --- a/includes/group-sites.php +++ b/includes/group-sites.php @@ -1820,9 +1820,10 @@ function( $v ) use ( $source_site_url, $source_site_upload_dir, $dest_site_url, } // Updated custom nav menu items - $locations = get_theme_mod( 'nav_menu_locations' ); - $menu_id = isset( $locations['primary'] ) ? (int) $locations['primary'] : 0; - $nav_items = get_term_meta( $menu_id, 'cboxol_custom_menus', true ); + $primary_nav_key = cboxol_get_theme_primary_nav_menu_location(); + $locations = get_theme_mod( 'nav_menu_locations' ); + $menu_id = isset( $locations[ $primary_nav_key ] ) ? (int) $locations[ $primary_nav_key ] : 0; + $nav_items = get_term_meta( $menu_id, 'cboxol_custom_menus', true ); if ( $menu_id && ! empty( $nav_items ) ) { $group_type = cboxol_get_group_group_type( $group_id ); @@ -2127,6 +2128,32 @@ function cboxol_get_nav_menu_items() { return $items; } +/** + * Determine a theme's primary nav menu location. + * + * Uses a heuristic based on naming conventions, and falls back on the first available. + * + * @since 1.6.0 + * + * @return string|null + */ +function cboxol_get_theme_primary_nav_menu_location() { + $keys_to_check = [ 'primary', 'main', 'header', 'top' ]; + + $locations = get_nav_menu_locations(); + if ( ! $locations ) { + return null; + } + + foreach ( $keys_to_check as $key ) { + if ( isset( $locations[ $key ] ) ) { + return $key; + } + } + + return key( $locations ); +} + /** * Register meta box for CBOX OpenLab nav menu. * diff --git a/includes/site-template.php b/includes/site-template.php index fc537a0..9f3cc7c 100644 --- a/includes/site-template.php +++ b/includes/site-template.php @@ -665,8 +665,9 @@ function cboxol_create_site_for_template( $template_id, $slug, $name ) { true ); - $locations = get_theme_mod( 'nav_menu_locations' ); - $locations['primary'] = $menu_id; + $primary_nav_key = cboxol_get_theme_primary_nav_menu_location(); + $locations = get_theme_mod( 'nav_menu_locations' ); + $locations[ $primary_nav_key ] = $menu_id; set_theme_mod( 'nav_menu_locations', $locations ); restore_current_blog();