# Shared Navigation Menu From the Main Site

> Adds a [wpide_network_menu] shortcode that displays a menu from the main site on any site in the network, so one menu can be maintained in one place.

- Type: PHP
- Runs: Run everywhere
- Category: [Multisite](https://wpide.com/library/category/multisite/)
- Tags: menus, shared, shortcode
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/ms-shared-main-site-menu/

## Code

```php
if ( ! is_multisite() ) {
    return;
}

add_shortcode( 'wpide_network_menu', function ( $atts ) {
    $atts = shortcode_atts( array( 'menu' => '', 'class' => 'wpide-network-menu' ), $atts, 'wpide_network_menu' );

    if ( '' === $atts['menu'] ) {
        return '';
    }

    $key  = 'wpide_net_menu_' . md5( $atts['menu'] . '|' . $atts['class'] );
    $html = get_site_transient( $key );

    if ( false === $html ) {
        switch_to_blog( get_main_site_id() );
        $html = wp_nav_menu( array(
            'menu'            => $atts['menu'],
            'container'       => 'nav',
            'container_class' => sanitize_html_class( $atts['class'] ),
            'container_aria_label' => __( 'Network' ),
            'fallback_cb'     => false,
            'echo'            => false,
        ) );
        restore_current_blog();
        $html = (string) $html;
        set_site_transient( $key, $html, HOUR_IN_SECONDS );
        $keys   = (array) get_site_option( 'wpide_net_menu_keys', array() );
        $keys[] = $key;
        update_site_option( 'wpide_net_menu_keys', array_unique( $keys ) );
    }

    return $html;
} );

add_action( 'wp_update_nav_menu', function () {
    if ( ! is_main_site() ) {
        return;
    }
    foreach ( (array) get_site_option( 'wpide_net_menu_keys', array() ) as $key ) {
        delete_site_transient( $key );
    }
    delete_site_option( 'wpide_net_menu_keys' );
} );
```

## Notes

Use [wpide_network_menu menu="Main Menu"] with the menu's name, slug or ID on the main site. The HTML is cached for an hour and cleared when a menu on the main site is saved.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Shared Navigation Menu From the Main Site" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
