Instantly share code, notes, and snippets.
Last active
March 16, 2021 08:57
-
Star
(3)
3
You must be signed in to star a gist -
Fork
(1)
1
You must be signed in to fork a gist
-
Save sirchrispy/13dee98ab22cfd714f414f6cdb18494e to your computer and use it in GitHub Desktop.
WordPress MultiSite and Genesis: Display a Network-Wide Menu Raw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This code adds a network-wide menu in Genesis Header Right. | |
* It can be altered to be in any location your heart desires. | |
* | |
* For this code to work, your main site and all sub-sites need to meet the following conditions: | |
* 1) Register a menu with the same name on all themes used in network | |
* 2) Create a menu via the Appearance menu on main site and sub-sites and assign it to the same location | |
* 3) Leave the menu created in condition 2 blank on all sub-sites | |
*/ | |
/* | |
* STEP 1 | |
* | |
* Place this code in your themes' functions.php file | |
*/ | |
// Unregister header-right widget area because we're going to replace it with our own | |
unregister_sidebar( 'header-right' ); | |
/* | |
* STEP 2 | |
* | |
* Place this code in your themes' functions.php file or a network-activated plugin. | |
* It could also be in an mu-plugin only if all themes on the network are Genesis | |
*/ | |
// Create desired theme menus | |
add_theme_support( 'genesis-menus', array( | |
// adds a header menu, but you can name it what you want | |
// this is the one I'm using as the network-wide menu | |
// as long as this has the same name on all the themes, it satisfies condition 1 | |
'header' => __( 'Header Navigation Menu', 'tcd' ), | |
// the default Genesis menus | |
'primary' => __( 'Primary Navigation Menu', 'tcd' ), | |
'secondary' => __( 'Secondary Navigation Menu', 'tcd' ), | |
) ); | |
// Only use this if your new menu isn't named 'header' (Genesis already does this for 'header') | |
// This adds the default genesis attributes to your menu (itemscope and itemtype) | |
// See genesis/lib/functions/markup.php, ~ line 420 | |
add_filter( 'genesis_attr_nav-footer', 'genesis_attributes_nav' ); | |
// Place the header menu in the header-right widget area | |
add_action( 'genesis_header_right', 'tcd_add_header_menu' ); | |
function tcd_add_header_menu() { | |
// do nothing if menu not supported | |
if ( !genesis_nav_menu_supported( 'header' ) ) | |
return; | |
// assign an extra class if using superfish | |
$class = 'menu genesis-nav-menu menu-header'; | |
if ( genesis_superfish_enabled() ) { | |
$class .= ' js-superfish'; | |
} | |
// define the genesis_nav_menu parameters, including where to display it | |
$args = array( 'theme_location' => 'header', 'menu_class' => $class ); | |
// display main site's header menu on sub-sites | |
if ( !is_main_site() ) { | |
// switch to main site | |
switch_to_blog(1); | |
// display header menu from main site | |
genesis_nav_menu( $args ); | |
// go back to displaying sub-site content | |
restore_current_blog(); | |
} | |
// display main site header menu | |
genesis_nav_menu( $args ); | |
} | |
// Remove the header menu option from sub-sites if not a super admin | |
add_action( 'admin_init', 'tcd_nav_menu_init' ); | |
function tcd_nav_menu_init() { | |
if ( !is_main_site() && !is_super_admin() ) | |
unregister_nav_menu( 'header' ); | |
} | |
/* | |
* OPTIONAL: STEP 3 | |
* | |
* This code gives your new menu Accessibility data, place it in the same file as you do the code in STEP 2 | |
* Modified from: https://gist.github.com/robincornett/7e6e4aa5317fd89e2cef#file-functions-02-php | |
*/ | |
// Modify the array Genesis provides by adding ID and Aria-label | |
// Genesis already adds itemscope & itemtype for menus called primary, secondary, header | |
// For more info, see the genesis/lib/functions/markup.php file, ~ line 407 | |
add_filter( 'genesis_attr_nav-header', 'tcd_modify_header_nav_attributes' ); | |
function tcd_modify_header_nav_attributes( $attributes ) { | |
// need this so skip link has a place to go | |
$attributes['id'] = 'genesis-nav-header'; | |
// labels the navigation for screen readers | |
$attributes['aria-label'] = __( 'Header Navigation', 'tcd' ); | |
return $attributes; | |
} | |
// Add support for accessibility functions | |
add_theme_support( 'genesis-accessibility', array( '404-page', 'drop-down-menu', 'headings', 'rems', 'search-form', 'skip-links' ) ); | |
// Modify the skip links to include the header navigation | |
add_filter( 'genesis_skip_links_output', 'tcd_modify_skip_links' ); | |
function tcd_modify_skip_links( $links ) { | |
// Assign old array to a new one and indicate where new data will be in the array | |
$add_links = $links; | |
array_splice( $add_links, 0 ); // change the 0 to 1 for 2nd position, etc. | |
// check to see if the theme supports and has a header menu | |
if ( genesis_nav_menu_supported( 'header' ) && has_nav_menu( 'header' ) ) { | |
// add the skip link ID and descriptive text to the array | |
$add_links['genesis-nav-header'] = __( 'Skip to header navigation', 'tcd' ); | |
} | |
// check to see if the theme supports and has a primary menu | |
// only need to use this if array_splice position set to 0 | |
if ( genesis_nav_menu_supported( 'primary' ) && has_nav_menu( 'primary' ) ) { | |
// the skip link ID and descriptive text to the array | |
$add_links['genesis-nav-primary'] = __( 'Skip to primary navigation', 'genesis' ); | |
} | |
// return the modified skip link array | |
return array_merge( $add_links, $links ); | |
} | |
/* | |
* OPTIONAL: STEP 4 | |
* | |
* Adds the new menu to the responsive menu | |
* Place in same location as the code from STEP 2 | |
* Note: this uses the responsive menu script from the Genesis Sample Theme 2.3.0 | |
* This function is enqueued into your scripts (see Genesis Sample Theme) | |
*/ | |
// Define responsive menu settings. | |
function tcd_responsive_menu_settings() { | |
$settings = array( | |
'mainMenu' => __( 'Menu', 'tcd' ), | |
'menuIconClass' => 'dashicons-before dashicons-menu', | |
'subMenu' => __( 'Submenu', 'tcd' ), | |
'subMenuIconsClass' => 'dashicons-before dashicons-arrow-down-alt2', | |
'menuClasses' => array( | |
'combine' => array( | |
// responsive menu visual order | |
// skip links will only go to first one, since all menus merge into one | |
'.nav-header', | |
'.nav-primary', | |
'.nav-secondary' | |
), | |
'others' => array(), | |
), | |
); | |
return $settings; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment