Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to add a menu that belongs to parent multisite blog to all child-blogs. I need the menu displayed in all child-blogs, I mean the same menu on all multisites blogs. How I can do that?

share|improve this question

2 Answers 2

Use a plugin header to force network activation:

Network: true

Register the menu page on admin_menu and network_admin_menu.

Sample code:

<?php
/**
 * Plugin Name: Network Wide Admin Menu
 * Network: true
 */
add_action( 'admin_menu',         'nwam_add_menu' );
add_action( 'network_admin_menu', 'nwam_add_menu' );

/**
 * Register an admin menu item.
 *
 * @wp-hook admin_menu
 * @wp-hook network_admin_menu
 * @return void
 */
function nwam_add_menu()
{
    add_menu_page(
        'Page Title',
        'Menu Title',
        'exist',
        'nwam',
        'nwam_render'
    );
}
/**
 * Callback to render the menu page.
 *
 * @return void
 */
function nwam_render()
{
    print '<h1>' . $GLOBALS['title'] . '</h1>';
}

The menu will be accessible now on all child sites and on the network management site:

enter image description here


In answer to your comment: To add an item to all navigation menus use the code from this answer in a plugin with a header Network: true.

share|improve this answer
    
Great, but is not I want. I was not referring to the admin menu, but rather to the menu on the frontend. Any idea? –  Marco Godínez Jan 30 '13 at 5:44
    
@markotom Sorry, I misread your question. :) See my update. –  toscho Jan 30 '13 at 5:49
    
I don't know, really doesn't help. I need to repeat the menu defined in parent blog to child blog, but no add arbitrarily new items to principal navigation, because no works if I change constantly the menu defined in parent blog... I'll have two menus unequal and desynchronized. I think the plugin shouldn't change every time the menu changes. –  Marco Godínez Jan 30 '13 at 7:09
up vote 0 down vote accepted

Well, thanks to @toscho ... For your help I found a way to achieve show the primary nav that belongs to parent blog to all child blogs:

/**
 * Plugin Name: Network Primary Nav
 * Network: true
 */

add_filter( 'wp_nav_menu_objects', 'network_primary_nav', 100, 2 );

function network_primary_nav( $menu_items, $args )
{
  global $blog_id;
  $menu_name = 'primary';

  if ( ( $blog_id > 1 ) && $menu_name == $args->theme_location )
  {

    // to parent blog
    switch_to_blog(1);

    $locations = get_nav_menu_locations();

    // get primary nav of parent blog
    if ( isset( $locations[ $menu_name ] ) )
    {
      $menu       = wp_get_nav_menu_object( $locations[ $menu_name ] );
      $menu_items = wp_get_nav_menu_items( $menu->term_id);
    }

    // to child blog
    restore_current_blog();

  }

  return $menu_items;

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.