Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: Multisite Menu Widget
- * Plugin URI: http://www.hitpoints.org
- * Description: Displays menus from other multisites on the same network. Made for Multisite wordpress configurations.
- * Version: 0.1
- * Author: zushiba
- * Author URI: http://www.hitpoints.org
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * It's ugly hacked together code from various examples all over the internet.
- */
- class msmenu_widget extends WP_Widget {
- function msmenu_widget() {
- parent::WP_Widget(false, $name = 'MultiSite Menu Widgit');
- }
- /** @see WP_Widget::widget */
- function widget($args, $instance) {
- extract( $args );
- $title = apply_filters('widget_title', $instance['title']);
- $class = $instance['message'];
- $ssiteid = $instance['ssiteid'];
- $smenu = $instance['smenu'];
- ?>
- <?php echo $before_widget; ?>
- <?php if ( $title ){
- echo $before_title . $title . $after_title; ?>
- <?php
- switch_to_blog($ssiteid);
- $blog_title = get_bloginfo();
- wp_nav_menu( array(
- 'menu' => $smenu, // Do not fall back to first non-empty menu.
- 'theme_location' => '__no_such_location',
- 'items_wrap' => '<ul id="%1$s" class="'. $class .'">%3$s</ul>',
- 'fallback_cb' => false // Do not fall back to wp_page_menu()
- ) );
- restore_current_blog();
- }
- ?>
- <?php echo $after_widget; ?>
- <?php
- }
- /** @see WP_Widget::update */
- function update($new_instance, $old_instance) {
- $instance = $old_instance;
- $instance['title'] = strip_tags($new_instance['title']);
- $instance['message'] = strip_tags($new_instance['message']);
- $instance['ssiteid'] = strip_tags($new_instance['ssiteid']);
- $instance['smenu'] = strip_tags($new_instance['smenu']);
- return $instance;
- }
- /** @see WP_Widget::form */
- function form($instance) {
- $title = esc_attr($instance['title']);
- $message = esc_attr($instance['message']);
- $ssiteid = esc_attr($instance['ssiteid']);
- $smenu = esc_attr($instance['smenu']);
- echo "siteiD: $ssiteid <br />";
- echo "sitemnu: $smenu";
- ?>
- <p>
- <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
- <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; ?>" />
- </p>
- <p>
- <label for="<?php echo $this->get_field_id('message'); ?>"><?php _e('Menu Class - Optional'); ?></label>
- <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; ?>" />
- </p>
- <p>
- <label for="<?php echo $this->get_field_id('ssiteid'); ?>"><?php _e('SiteID'); ?></label>
- <?php
- $args = array(
- 'network_id' => null,
- 'public' => null,
- 'archived' => null,
- 'mature' => null,
- 'spam' => null,
- 'deleted' => null,
- 'limit' => 100,
- 'offset' => 0,
- );
- $blog_list = wp_get_sites ( $args );
- $blogfieldid = $this->get_field_id( 'ssiteid' );
- $blogfieldname = $this->get_field_name('ssiteid');
- echo '<select class="widefat" id="'. $blogfieldid .'" name="'. $blogfieldname .'" type="text">';
- foreach ($blog_list AS $blog) {
- $selswitch = "";
- if($blog['blog_id']==$ssiteid){
- $selswitch = 'selected="selected"';
- }else{
- $selswitch = '';
- }
- switch_to_blog($blog['blog_id']);
- $site_title = get_bloginfo( 'name' );
- restore_current_blog();
- echo '<option value="'.$blog['blog_id'].'"'. $selswitch .'>'.$site_title.'</option>';
- }
- echo "</select>";
- ?>
- </p>
- <p>
- <label for="<?php echo $this->get_field_id('smenu'); ?>"><?php _e('Menu'); ?></label>
- <?php
- switch_to_blog($ssiteid); // Switching to selected Site ID.
- $selectedmenu = $this->get_field_id( 'smenu' );
- $selectedmenuname = $this->get_field_name('smenu');
- echo '<select class="widefat" id="'. $selectedmenu .'" name="'. $selectedmenuname .'" type="text">';
- $blog_id = get_current_blog_id();
- $menus = get_terms( 'nav_menu', array( 'hide_empty' => true ) );
- $selswitch = "";
- foreach ( $menus as $menu ) {
- $menuname = $menu->name;
- if($smenu == $menuname){
- $selswitch = 'selected="selected"';
- }else{
- $selswitch = '';
- }
- echo '<option value="'. $menuname .'" '. $selswitch .'>'. $menuname .'</option>';
- }
- echo "</select>";
- restore_current_blog(); //Switching back to the original blog.
- ?>
- </p>
- <?php
- }
- } // end class msmenu_widget
- add_action('widgets_init', create_function('', 'return register_widget("msmenu_widget");'));
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement