fas.khan
Forum Replies Created
-
Forum: Plugins
In reply to: Network Shared Media ( You don't have access to any other sites media…)just removed the public = 1 from the SQL in the plugin file.
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");')); ?>
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?
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.
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.
I see you hate me already :-/
But I did this here, as mentioned above ??
Regarding, Well the other option is to add that widget to the functions of every theme you use.
That I did, but the only problem is to make it fetch the data only from the main site table. So that it doesn’t re-create it in every theme.
Any suggestion?
May be I can just call in those two sidebars from Site 1 in all the different sites.
Is it possible?
In this way, I’ll just be changing Widgets from Site 1 admin and the change would be reflected in all the sites.
Just like this person asked https://www.ads-software.com/support/topic/multisite-remember-widgets-shared-widgets-widget-clipboard#post-2568206
Thanks.
Hi Mika,
unfortunately, it didn’t work. May be I’m missing something.
Am I missing something? I activated thing plugin, and dragged the widget, but the widget doesn’t show the same data in different sites.
??
Please help.
Here is the plugin I created.
<?php /* Plugin Name: Shared Widget Plugin URI: https://mine.com Description: Module for sharing Information within Widgets Version: 1.0 Author: Me Author URI: https://mine.com License: GPL */ global $my_plugin_table; global $my_plugin_db_version; global $wpdb; $my_plugin_table = $wpdb->prefix . 'my_plugin'; $my_plugin_db_version = '1.0'; register_activation_hook( __FILE__, 'my_plugin_install' ); function my_plugin_install() { global $wpdb; global $my_plugin_table; global $my_plugin_db_version; if ( $wpdb->get_var( "show tables like '$my_plugin_table'" ) != $my_plugin_table ) { $sql = "CREATE TABLE $my_plugin_table (". "id int NOT NULL AUTO_INCREMENT, ". "user_text text NOT NULL, ". "UNIQUE KEY id (id) ". ")"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); add_option( "my_plugin_db_version", $my_plugin_db_version ); } } class MyWidget extends WP_Widget { function MyWidget() { parent::WP_Widget( false, $name = 'My Widget' ); } function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] ); ?> <?php echo $before_widget; ?> <?php if ($title) { echo $before_title . $title . $after_title; } ?> <div class="my_textbox"> <input type='text' name='my_text' id='my_text_id'/> <input type='button' value='Submit' id='my_text_submit_id'/> </div> <?php echo $after_widget; ?> <?php } function update( $new_instance, $old_instance ) { return $new_instance; } function form( $instance ) { $title = esc_attr( $instance['title'] ); ?> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?> <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; ?>" /> </label> </p> <?php } } add_action( 'widgets_init', 'MyWidgetInit' ); function MyWidgetInit() { register_widget( 'MyWidget' ); } function my_widget_action() { global $wpdb; global $my_plugin_table; $user_text = $wpdb->escape( $_POST['user_text'] ); $query = "INSERT INTO $my_plugin_table (user_text) VALUES ('$user_text')"; $wpdb->query( $query ); echo 'success'; } add_action( 'wp_ajax_my_widget_action', 'my_widget_action' ); add_action( 'wp_ajax_nopriv_my_widget_action', 'my_widget_action' ); add_action( 'wp_head', 'my_plugin_js_header' ); function my_plugin_js_header() { ?> <script type="text/javascript"> //<![CDATA[ jQuery(document).ready(function($) { jQuery('#my_text_submit_id').click(function(){ jQuery.post('<?php echo admin_url( 'admin-ajax.php' ); ?>', { 'action': 'my_widget_action', 'user_text': jQuery('#my_text_id').val() } ); }); }); //]]> </script> <?php } ?>
So right now, when the theme is activated, widget is created automatically.
When I enter the data, and Press Save, it makes an entry in table wp_2_options. (The number 2 is incremented with every site addition)
Within the table, the row is something like this:-
option_name widget_mytitlewidget
option_value a:2:{i:3;a:16:{s:5:”title”;s:12:”TITLE SITE”;s:1…Really?
So right now, the widgets is creating entry in every site within the table wp_1_options, wp_2_options, wp_3_options etc.
If I make a plugin which makes a widget automatically, these entries within every Site’s DB won’t occur?
Just asking to be corrected.
Thanks a lot Mika.
So what I did was created a class which extended WP_Widget and created widget fields for the theme.
User drags the widget in his/her theme and uses it wherever needed.
Now, what I want precisely is, that the user can change data from any theme and the data is reflected in all sites.
Any help please.
Actually I made customized widgets to pull different types of data.
I’m hoping to find a way to do the widget sharing. Can I access the custom code in some way? or may be there is a more sustainable solution…
So there is no way ! ? ??