• 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.

Viewing 15 replies - 1 through 15 (of 25 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Not easily. Widgets are per site (and often controlled by themes and plugins). It’s generally easier (and better) to set ’em up per site.

    Thread Starter fas.khan

    (@faskhan)

    So there is no way ! ? ??

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    You would have to custom code your theme to pull the widgets from the main site, which will slow your site down in the long run.

    When you say ‘personalized widgets’ do you just mean that you used a normal widget and customized it in the Widgets page?

    Thread Starter fas.khan

    (@faskhan)

    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…

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    If you made the widget code into a plugin, you could activate it on all sites.

    But you’re a little vague here on what your widgets are. In general, sites are per-site. Your configurations and content are independent.

    Thread Starter fas.khan

    (@faskhan)

    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.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Okay, so the code is in your theme’s function.php, right?

    Take it out and put it in a plugin instead ?? Put that plugin in mu-plugins and you’re done.

    Thread Starter fas.khan

    (@faskhan)

    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.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    I’ll ask you a question back ??

    If you activate the theme where you have the widget on another site, where will the options be created?

    Thread Starter fas.khan

    (@faskhan)

    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…

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    The same thing happens in a plugin ??

    You may have to put it in as a normal plugin and activate it per-site, but in theory it should work just fine the first time someone visits the widgets page.

    Unless you specified wp_2_options, WordPress is smart ?? It knows wp_ME_options ??

    Thread Starter fas.khan

    (@faskhan)

    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
    }
    
    ?>
    Thread Starter fas.khan

    (@faskhan)

    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.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Well the other option is to add that widget to the functions of every theme you use.

    Thread Starter fas.khan

    (@faskhan)

    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?

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘Want my widgets to run in all the sites, within my multisite install’ is closed to new replies.