The MU forums have moved to WordPress.org

Grabbing a widget's contents from a different blog (13 posts)

  1. r-a-y
    Member
    Posted 14 years ago #

    The topic is self-explanatory.

    I want to be able to grab the contents of a widget sidebar from an MU blog and have the ability to output its contents via PHP, so I can insert it into a Wordpress theme meant for secondary MU blogs.

    Is this possible?

  2. dsader
    Member
    Posted 14 years ago #

    You could wrap the call to a dynamic_sidebar in switch_to_blog and restore_current_blog WPMU functions.

    In this example the widgets on sidebar 1 of blog 1 will display on any blog with the same theme active:

    switch_to_blog(1); // main blog sidebar widgets override user blog
    if ( function_exists('dynamic_sidebar') && dynamic_sidebar(1) ) : else :
     endif;
    restore_current_blog();
  3. r-a-y
    Member
    Posted 14 years ago #

    dsaber, that sounds like it will work!

    I'll give it a shot.
    Thanks!

  4. r-a-y
    Member
    Posted 14 years ago #

    Hey dsader,

    Just tried the switch_to_blog() function for a widget sidebar on a sub-blog and it doesn't seem to work.

    Any idea why?

    I'm still on WPMU 2.7.1 so that shouldn't affect the problems with 2.8.1.

    [EDIT]
    I just re-read your post, dsader.
    So it has to be the same theme across all blogs?

    I am using a slightly-modified theme of blog #1 for the rest of the blogs. So I guess that's why it isn't working...

    I am using the same functions.php file though, so why wouldn't it work?

    Is there a workaround?

  5. dsader
    Member
    Posted 14 years ago #

    Well the same theme doesn't matter, paste the same code in any themes sidebar.php you like. The functions.php shouldn't have anything to do with it, unless widgets are not enabled.

    I just tried this again, WPMU 2.7.1.

    First, I have widgets active and options saved on blog 1.

    Then, I paste the code above into a sidebar.php of a theme active on, say, blog 63.

    The front end widgets, and options, from blog 1 appear on blog 63's sidebar. In my case, just above blog 63's usual widgets.

    I edit the widget options and save them while on blog 1 backend, and the changes are reflected on blog 63's sidebar frontend, as expected.

  6. r-a-y
    Member
    Posted 14 years ago #

    Widgets are enabled and active on blog #1.
    I'm using a widget sidebar in the footer.

    The widget bar shows up on blog #1 as expected.

    I'm attempting to use the same widget bar in the footer of blog #2.
    Using the switch_to_blog() code there, but nothing is showing up.

    I've tried different variants of the code as well:

    <?php switch_to_blog(1); // main blog sidebar widgets override user blog
    if ( function_exists('dynamic_sidebar') && dynamic_sidebar(2) ) : else :
     endif;
    restore_current_blog(); ?>

    and
    <?php switch_to_blog(1); if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer') ) : ?><?php endif; restore_current_blog(); ?>

    I've added and removed the widget code in blog #2's functions.php from blog #1's functions.php and both do nothing.

    Any other ideas?

  7. landykos
    Member
    Posted 14 years ago #

    I have been trying to figure this out for awhile now. I really wish this could be built into MU.

    I had this working but now on 2.8.1 it doesn't work anymore.

    Any ideas why this would not work since the upgrade?

  8. andrea_r
    Moderator
    Posted 14 years ago #

    Because they wrote a new widget class for 2.8. Supposed to be backwards compatible, but you never know.

  9. tomgf
    Member
    Posted 14 years ago #

    I am also trying to do this on WPMU 2.8.4a. So far, unsuccessfully.

    switch_to_blog() seems to be working in other areas but not on widgets…

    Any thoughts about finding a way to fix it…?

  10. r-a-y
    Member
    Posted 14 years ago #

    I still have the same problems on 2.7.1; I can do WP_Query loops no problem with the switch_to_blog() function, but like you stated tomgf, it doesn't seem to work for widgets (in our case anyway!).

  11. fakup
    Member
    Posted 13 years ago #

    Hello! Did someone find a solution? I'm using "My Calendar" plugin on the home blog and want to show the "today events" and "future events" widget's results on the other blogs. Not working with WPMU 2.9.2

  12. jimisaacs
    Member
    Posted 13 years ago #

    I've fought with this for some time now.

    The problem is that the widget API is not MU aware. Everything is based on global variables and each time you switch to a blog you overwrite those globals.

    If this is okay, a solution is to grab all the widgets from a particular blog then continue onward. There will be interferences, and widget discrepancies, so I would research more if you intend to to it. I had a half hazard solution a while back using lots and lots of object buffers to grab the widget output before the page output, then put it where it needs to go later in the script.This is really the way widgets need to be done to begin with, but I hate bandaging stuff like this, and would rather start at root of the problem.

    Overall, I am quite done debugging this, and would instead like to help in overhauling the widget API.

    In your functions file (before the action 'widgets_init' is run)

    switch_to_blog(1); // or what ever blog you need
    wp_widgets_init();
    dynamic_sidebar();
    restore_current_blog();

    You can simply put the above code in a blog loop, and wrap the iderations in a buffer like I said before. Then reference that output later as variables.

    The above works well for MU site footers though. Say, for an MU site with a global footer, you don't need to worry about what things get overwritten by the time the script has reached the footer.

  13. andrea_r
    Moderator
    Posted 13 years ago #

    The problem is that the widget API is not MU aware.

    I'd like to disagree with this - the way widgets are setup are *precisely* because of MU. They are meant to be blog-specific, not global.

    If they were global, then I'd say they were not mu-aware. Think of wordpress.com here.

    Overall, I am quite done debugging this, and would instead like to help in overhauling the widget API.

    Then get thee over to wordpress trac and start filing tickets and digging in.

    The above works well for MU site footers though. Say, for an MU site with a global footer, you don't need to worry about what things get overwritten by the time the script has reached the footer.

    Wrong use for the wrong job. If I were doing a global footer than I'd, you know, hook into the footers.

About this Topic