Want my widgets to run in all the sites, within my multisite install
-
Hi,
I’ve made few personalized Widgets for my blog and was wondering if there was a way to use them throughout the sites.I want to use them in all the sites of my WP install.
Please help.
-
But I did this here, as mentioned above ??
I see you hate me already :-/
No, I just was out tonight. I hate very few people ??
I wasn’t sure you’d read that reasource. The tl;dr of it is that you’ve made the plugin wrong, and you should be calling options in a non-table specific way. By using the defines built into WP, you can make a plugin generic enough to work on WP. You’ll have to look up how to do that.
Yay! Mika doesn’t hate me!
Oh! So I guess that’s exactly what I have been missing.
In the moaning period though, when I was waiting for your reply, my heartbroken search results yielded me to this plugin yd-wpmu-sitewide-options. It kinda did what I was looking for.
But I’d still be working on the plugin as you suggested.
Thanks a lot Mika, you have now officially saved my ass. Twice.
Best.
Hi Mika,
Bad News, that plugin doesn’t work exactly the way I wanted. I wanted to share the content and it shares Settings.
So back to sqaure 1.
Regarding making a plugin that saves data in a generic way throughout all the franchise; do you think you know any resource or place where it’s shown how to do that?
Andrea also mentions it here.
I’m facing quite a challenge doing that …
Thanks a lot.
I wanted to share the content and it shares Settings.
Oh. The content
That’s always per site. You’d have to custom code it to always pull the content from one site. Bleah. You could perhaps try to save it as a
add_site_option()
settings. That’s where network wide settings are saved.Like
add_site_option('my_cool_widget_content','theconent');
And have it configured from the network admin not per site. So anyone can use the widget, but master control is on the network?
I have a plugin called Sitewide Comment Control that may give you ideas how to manage the data. Then just pull those option values into your widget.
Thanks Mika,
So, within form function (//public function form( $instance )), I should be using add_site_option($key,$value)
Within, function update( $new_instance, $old_instance ), I’ll be using update_site_option($key,$value)
and within function widget( $args, $instance ), I’ll be using get_site_option($key)
right?
At that, I’m not sure. I’m not the best with Widget code ??
Hi Mika,
Thank you for all your assistance.
I’ve successfully made a plugin, which makes a Widget and then shares the content among all sites. The content can only be changed from the Main Site. (The first made blog.)
I know there is no solution of it on internet, so I’ll copy the code below whoever needs it.
Right now, I couldn’t make a generic one for whatever Widget you can create and then share its content. I hardcoded the widgets, according to the fields I wanted in the code. I’ll be trying to create a code that can first create widgets through Admin and then share it’s contents all over sites. For now, you can use the code (by just copying it in plugins folder) by tweaking it according to the fields you need.
To start, Just copy the following code in plugins folder, activate the plugin and you’re good to go.
<?php /* Plugin Name: Shared Modules Enabler Plugin URI: https://asdf.com Description: Allows Sharing of Content among Widgets. Data can only be changed within one site (Main Site) Author: Faisal Shabir Version: 1.0.2 Author URI: https://asdf.com */ class ShareWidget1 extends WP_Widget { /** constructor */ function ShareWidget1() { parent::WP_Widget(false, $name = 'Share Widget 1'); } /** @see WP_Widget::widget */ function widget($args, $instance) { extract( $args ); //$title = apply_filters('widget_title', $instance['title']); $title = get_site_option('Share_Widget_1_Title'); echo $before_widget; echo $before_title; echo $title; echo $after_title; echo "<div class=\"mod products\">"; echo "<ul class=\"suggest-img\">"; /* for ($i = 1; $i <= 3; $i++) { if ($instance['item'.$i.'title']){ echo "<li class=\"clearfix\">"; echo "<a href=\"" . $instance['item'.$i.'url'] ."\"><img class=\"shapeup\" border=\"0px\" src=\"" . $instance['item'.$i.'thumbnail'] ."\" /></a>"; echo "<div class=\"content\">"; echo "<h4><a href=\"" . $instance['item'.$i.'url'] ."\">" . $instance['item'.$i.'title'] . "</a></h4>"; echo "<p>" . $instance['item'.$i.'description']."</p>"; echo "<p class=\"price\">" . $instance['item'.$i.'price']."</p>"; echo "</div>"; echo "</li>"; } }*/ //get_site_option('Share_Widget_1_item'.$j.'title'); for ($i = 1; $i <= 3; $i++) { if (get_site_option('Share_Widget_1_item'.$i.'title')){ echo "<li class=\"clearfix\">"; echo "<a href=\"" . get_site_option('Share_Widget_1_item'.$i.'url') ."\"><img class=\"shapeup\" border=\"0px\" src=\"" . get_site_option('Share_Widget_1_item'.$i.'thumbnail') ."\" /></a>"; echo "<div class=\"content\">"; echo "<h4><a href=\"" . get_site_option('Share_Widget_1_item'.$i.'url') ."\">" . get_site_option('Share_Widget_1_item'.$i.'title') . "</a></h4>"; echo "<p>" . get_site_option('Share_Widget_1_item'.$i.'description')."</p>"; echo "<p class=\"price\">" . get_site_option('Share_Widget_1_item'.$i.'price')."</p>"; echo "</div>"; echo "</li>"; } } echo "</ul>"; echo "<div class=\"clearfix see-more\"><a class=\"pill pill-see-more\" href=\"https://google.com\">See More</a></div>"; echo "</div>"; echo $after_widget; } /** @see WP_Widget::update */ function update($new_instance, $old_instance) { global $blog_id; if($blog_id == 1) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); update_site_option('Share_Widget_1_Title',strip_tags($new_instance['title'])); for ($i = 1; $i <= 3; $i++) { $instance['item'.$i.'title'] = strip_tags($new_instance['item'.$i.'title']); update_site_option('Share_Widget_1_item'.$i.'title',strip_tags($new_instance['item'.$i.'title'])); $instance['item'.$i.'url'] = strip_tags($new_instance['item'.$i.'url']); update_site_option('Share_Widget_1_item'.$i.'url',strip_tags($new_instance['item'.$i.'url'])); $instance['item'.$i.'description'] = strip_tags($new_instance['item'.$i.'description']); update_site_option('Share_Widget_1_item'.$i.'description',strip_tags($new_instance['item'.$i.'description'])); $instance['item'.$i.'price'] = strip_tags($new_instance['item'.$i.'price']); update_site_option('Share_Widget_1_item'.$i.'price',strip_tags($new_instance['item'.$i.'price'])); $instance['item'.$i.'thumbnail'] = $new_instance['item'.$i.'thumbnail']; update_site_option('Share_Widget_1_item'.$i.'thumbnail',strip_tags($new_instance['item'.$i.'thumbnail'])); } return $instance; } } /** @see WP_Widget::form */ function form($instance) { //$title = esc_attr($instance['title']); $title = get_site_option('Share_Widget_1_Title'); for($i = 1; $i <= 3; $i++) { $itemTitles[$i] = $instance['item'.$i.'title']; $itemUrls[$i] = $instance['item'.$i.'url']; $itemDescriptions[$i] = $instance['item'.$i.'description']; $itemPrices[$i] = $instance['item'.$i.'price']; $itemThumbnails[$i] = $instance['item'.$i.'thumbnail']; } ?> <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> <?php $j=1; foreach($itemTitles as $num => $item) : ?> <p> <label for="<?php echo $this->get_field_id('item'.$num.'title'); ?>"><?php _e('Item ' . $num . ' Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('item'.$num.'title'); ?>" name="<?php echo $this->get_field_name('item'.$num.'title'); ?>" type="text" value="<?php echo //esc_attr($itemTitles[$num]); get_site_option('Share_Widget_1_item'.$j.'title'); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id('item'.$num.'url'); ?>"><?php _e('Item ' . $num . ' URL:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('item'.$num.'url'); ?>" name="<?php echo $this->get_field_name('item'.$num.'url'); ?>" type="text" value="<?php echo get_site_option('Share_Widget_1_item'.$j.'url'); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id('item'.$num.'description'); ?>"><?php _e('Item ' . $num . ' Description:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('item'.$num.'description'); ?>" name="<?php echo $this->get_field_name('item'.$num.'description'); ?>" type="text" value="<?php echo get_site_option('Share_Widget_1_item'.$j.'description'); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id('item'.$num.'price'); ?>"><?php _e('Item ' . $num . ' Price:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('item'.$num.'price'); ?>" name="<?php echo $this->get_field_name('item'.$num.'price'); ?>" type="text" value="<?php echo get_site_option('Share_Widget_1_item'.$j.'price'); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id('item'.$num.'thumbnail'); ?>"><?php _e('Item ' . $num . ' Thumbnail:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('item'.$num.'thumbnail'); ?>" name="<?php echo $this->get_field_name('item'.$num.'thumbnail'); ?>" type="text" value="<?php echo get_site_option('Share_Widget_1_item'.$j.'thumbnail'); ?>" /> </p> <?php $j++; endforeach; global $blog_id; if ($blog_id !=1) { echo "<br /><br /><span style='color:#900'>Seems like you are <strong>not</strong> in the Main Site.</span> <br />Don't be surprised when you press this Save Button and nothing happens, as you can only make changes through HITH's Admin<br /> Just saying.<br /> I know you're still gonna try. <br />I would have.<br /><br />"; } } } // class FooWidget add_action('widgets_init', create_function('', 'return register_widget("ShareWidget1");')); ?>
- The topic ‘Want my widgets to run in all the sites, within my multisite install’ is closed to new replies.