Advertisement
Guest User

Multisite Menu widget, wordpress

a guest
May 3rd, 2016
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.13 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Plugin Name: Multisite Menu Widget
  5.  * Plugin URI: http://www.hitpoints.org
  6.  * Description: Displays menus from other multisites on the same network. Made for Multisite wordpress configurations.
  7.  * Version: 0.1
  8.  * Author: zushiba
  9.  * Author URI: http://www.hitpoints.org
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14.  * It's ugly hacked together code from various examples all over the internet.
  15.  */
  16. class msmenu_widget extends WP_Widget {
  17.  
  18.  
  19.     function msmenu_widget() {
  20.         parent::WP_Widget(false, $name = 'MultiSite Menu Widgit'); 
  21.     }
  22.  
  23.     /** @see WP_Widget::widget */
  24.     function widget($args, $instance) {
  25.         extract( $args );
  26.         $title      = apply_filters('widget_title', $instance['title']);
  27.         $class  = $instance['message'];
  28.         $ssiteid    = $instance['ssiteid'];
  29.         $smenu  = $instance['smenu'];
  30.         ?>
  31.               <?php echo $before_widget; ?>
  32.                   <?php if ( $title ){
  33.                         echo $before_title . $title . $after_title; ?>
  34.                             <?php
  35.                             switch_to_blog($ssiteid);
  36.                             $blog_title = get_bloginfo();
  37.  
  38.                             wp_nav_menu( array(
  39.                             'menu'           => $smenu, // Do not fall back to first non-empty menu.
  40.                             'theme_location' => '__no_such_location',
  41.                             'items_wrap'     => '<ul id="%1$s" class="'. $class .'">%3$s</ul>',
  42.                             'fallback_cb'    => false // Do not fall back to wp_page_menu()
  43.                             ) );
  44.                             restore_current_blog();
  45.                   }
  46.                             ?>
  47.               <?php echo $after_widget; ?>
  48.         <?php
  49.     }
  50.  
  51.     /** @see WP_Widget::update */
  52.     function update($new_instance, $old_instance) {    
  53.         $instance = $old_instance;
  54.         $instance['title'] = strip_tags($new_instance['title']);
  55.         $instance['message'] = strip_tags($new_instance['message']);
  56.         $instance['ssiteid'] = strip_tags($new_instance['ssiteid']);
  57.         $instance['smenu'] = strip_tags($new_instance['smenu']);
  58.         return $instance;
  59.     }
  60.  
  61.     /** @see WP_Widget::form */
  62.     function form($instance) { 
  63.  
  64.         $title      = esc_attr($instance['title']);
  65.         $message    = esc_attr($instance['message']);
  66.         $ssiteid    = esc_attr($instance['ssiteid']);
  67.         $smenu      = esc_attr($instance['smenu']);
  68.         echo "siteiD: $ssiteid <br />";
  69.         echo "sitemnu: $smenu";
  70.         ?>
  71.          <p>
  72.           <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  73.           <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
  74.         </p>
  75.         <p>
  76.           <label for="<?php echo $this->get_field_id('message'); ?>"><?php _e('Menu Class - Optional'); ?></label>
  77.           <input class="widefat" id="<?php echo $this->get_field_id('message'); ?>" name="<?php echo $this->get_field_name('message'); ?>" type="text" value="<?php echo $message; ?>" />
  78.         </p>
  79.         <p>
  80.             <label for="<?php echo $this->get_field_id('ssiteid'); ?>"><?php _e('SiteID'); ?></label>
  81.             <?php
  82.                 $args = array(
  83.                     'network_id' => null,
  84.                     'public'     => null,
  85.                     'archived'   => null,
  86.                     'mature'     => null,
  87.                     'spam'       => null,
  88.                     'deleted'    => null,
  89.                     'limit'      => 100,
  90.                     'offset'     => 0,
  91.                 );
  92.                 $blog_list = wp_get_sites ( $args );
  93.                 $blogfieldid = $this->get_field_id( 'ssiteid' );
  94.                 $blogfieldname = $this->get_field_name('ssiteid');
  95.                 echo '<select class="widefat" id="'. $blogfieldid .'" name="'. $blogfieldname .'" type="text">';
  96.                 foreach ($blog_list AS $blog) {
  97.                     $selswitch = "";
  98.                     if($blog['blog_id']==$ssiteid){
  99.                         $selswitch = 'selected="selected"';
  100.                     }else{
  101.                         $selswitch = '';
  102.                     }
  103.                     switch_to_blog($blog['blog_id']);
  104.                         $site_title = get_bloginfo( 'name' );
  105.                     restore_current_blog();
  106.                
  107.                     echo '<option value="'.$blog['blog_id'].'"'. $selswitch .'>'.$site_title.'</option>';
  108.                 }
  109.                 echo "</select>";
  110.                 ?>
  111.         </p>
  112.         <p>
  113.             <label for="<?php echo $this->get_field_id('smenu'); ?>"><?php _e('Menu'); ?></label>
  114.         <?php
  115.                
  116.                 switch_to_blog($ssiteid); // Switching to selected Site ID.
  117.                 $selectedmenu = $this->get_field_id( 'smenu' );
  118.                 $selectedmenuname = $this->get_field_name('smenu');
  119.                
  120.                 echo '<select class="widefat" id="'. $selectedmenu .'" name="'. $selectedmenuname .'" type="text">';
  121.                
  122.  
  123.                 $blog_id = get_current_blog_id();
  124.                
  125.                 $menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );
  126.                 $selswitch = "";
  127.  
  128.                     foreach ( $menus as $menu ) {
  129.                         $menuname = $menu->name;
  130.                         if($smenu == $menuname){
  131.                             $selswitch = 'selected="selected"';
  132.                         }else{
  133.                             $selswitch = '';
  134.                         }
  135.                             echo '<option value="'. $menuname .'" '. $selswitch .'>'. $menuname .'</option>';
  136.                     }
  137.                     echo "</select>";
  138.  
  139.                 restore_current_blog();     //Switching back to the original blog.
  140.                 ?>
  141.                 </p>
  142.         <?php
  143.     }
  144.  
  145.  
  146. } // end class msmenu_widget
  147. add_action('widgets_init', create_function('', 'return register_widget("msmenu_widget");'));
  148. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement