WordPress.org

Ready to get started?Download WordPress

Forums

Sub-blogs use the main blog widgets (21 posts)

  1. justinbriggs1
    Member
    Posted 3 years ago #

    Hey everyone, I just had my multisite client give me an unusual request. They want each subblog's widgets to be controlled by the main blog, i.e. the sub-blog administrator will have no control over the widgets, and they will exactly reflect the widgets on the main one. Is there a way to do this? Thanks,

    Justin

  2. Hmm.

    If they're all using the same theme, the 'easiest' way is to hard code the sidebars/widgets. Of course, then you lose flexibility.

    In theory, you should be able to specify which site the widgets come from in the calls for the widget areas, and I swear I've seen this mentioned somewhere else before!

  3. justinbriggs1
    Member
    Posted 3 years ago #

    Hard coding is one option, and would work for most of the sidebar items, but the problem is that they have a couple of plugins, such as the advertising manager, that they want to retain control of, and will update globally across the main blog and all sub blogs. That would mean that I would have to somehow pull only the specified widgets from the main blog, and then hard code the other ones into the sidebar at certain positions.

    I wouldn't mind taking all of the widget control away from the sub-blog user, which is feasible, but I still don't know how to display the widgets from the main blog globally.

  4. switch_to_blog if you don't have a large number of sites.

    menus plugins to deny access to backend menus, or knock down the user privs.

  5. justinbriggs1
    Member
    Posted 3 years ago #

    That's exactly what I am looking for. I think it will be fine for our needs, I don't expect a lot of sub-blogs or traffic to any of the sites. I can't seem to get it to work, however, when I apply it to my dynamic sidebars. I have it set up like this in my sidebar.php file:

    <?php
    switch_to_blog(1);
    dynamic_sidebar( 'global-widget-area' );
    restore_current_blog();
    ?>

    Still getting a blank sidebar on the sub-blog. Any idea why?

  6. Is there a dynamic sidebar on the main blog called 'global-widget-area' ?

  7. justinbriggs1
    Member
    Posted 3 years ago #

    Yes, that is one I created myself, and it works on the main blog just fine, but not in the sub blog in this scenario

  8. Speggey
    Member
    Posted 3 years ago #

    I have a similar problem. I want to use a function from a plugin installed on the main blog on the sub blogs.

    switch_to_blog(1);
    wp_bannerize('group=right');
    restore_current_blog();

    It just shows up blank on the sub blogs but works on the main blog.

  9. justinbriggs1
    Member
    Posted 3 years ago #

    I have found a bunch of other resources and forum posts on this topic since I started this thread. There are several that specifically state that this is possible, but I haven't seen any evidence that switch_to_blog() works sidebars in this manner. Unless someone has a working example?

  10. David Sader
    Member
    Posted 3 years ago #

    In the following "mu-plugin"(or functions.php of common theme) example, I wanted a text widget and an rss widget as the only widgets on every site's sidebar. I can dynamically edit the widgets in the usual way on blog 1, yet the remaining blogs can't undo.

    All sites have same twentyten theme active so all sidebars have same name.

    <?php
    function override_sidebars_widgets() {
    	$bar = get_blog_option('1','sidebars_widgets');
    	return $bar;
    }
    function override_widget_rss() {
    	$bar = get_blog_option('1','widget_rss');
    	return $bar;
    }
    function override_widget_text() {
    	$bar = get_blog_option('1','widget_text');
    	return $bar;
    }
    	global $blog_id;
    	if($blog_id != '1') {
    add_filter( 'pre_option_' . 'sidebars_widgets', 'override_sidebars_widgets' );
    add_filter( 'pre_option_' . 'widget_rss', 'override_widget_rss' );
    add_filter( 'pre_option_' . 'widget_text', 'override_widget_text' );
    	}

    I suspect you could add your wp_bannerize option/widget in the same way.

  11. Speggey
    Member
    Posted 3 years ago #

    Hey, thanks for the reply.
    So I tried that with just a textwidget, couldn't get the main blogs widgets to show up on the sub blogs though.

    I've looked in the codex on "get_blog_option" and can't find any argument for 'sidebar_widgets'..

    Any ideas?

  12. David Sader
    Member
    Posted 3 years ago #

    I tried that with just a textwidget

    Both blogs using the same theme?
    The above snippet pasted into the functions.php of that same theme?
    The main blog id is '1'?
    You've added the text widget to blog 1?

    I just tried these steps again and it works for me with twentyten.

    I've looked in the codex on "get_blog_option" and can't find any argument for 'sidebar_widgets'.

    get_blog_option is as generic as get_option except you can specify which blog you get the options from. You'll need to look into your db wp_options table to see what options are there to dupe. Could be thousands to choose from, hundreds anyway.

  13. Speggey
    Member
    Posted 3 years ago #

    Both blogs using the same theme?
    The above snippet pasted into the functions.php of that same theme?
    The main blog id is '1'?
    You've added the text widget to blog 1?

    I just tried these steps again and it works for me with twentyten.

    Well the sub-blogs uses a child-theme of the main-blog. It's built on starkers 3 which is built on twenty-ten.
    Main blog id is '1' and I'm adding the text-widget to blog 1.
    Might have something to do with the child-theme? though it should use the same function.php file as I don't have one in the child-theme, and even if I did it would just extend the main theme functions.php, right?

    get_blog_option is as generic as get_option except you can specify which blog you get the options from. You'll need to look into your db wp_options table to see what options are there to dupe. Could be thousands to choose from, hundreds anyway.

    Oh ok, I will look into that, thanks!

  14. Speggey
    Member
    Posted 3 years ago #

    Ok so here's what I've tried:

    function override_sidebars_widgets() {
    	$bar = get_blog_option('1','sidebars_widgets');
    	return $bar;
    }
    
    function override_widget_wp_bannerize() {
    	$bar = get_blog_option('1','widget_wp_bannerize');
    	return $bar;
    }
    
    global $blog_id;
    if($blog_id != '1') {
    	add_filter( 'pre_update_option_' . 'sidebars_widgets', 'override_sidebars_widgets' );
    	add_filter( 'pre_option_' . 'widget_wp_bannerize', 'override_widget_wp_bannerize' );
    }

    pre_update_option since I still want to use widgets independently on the sub blogs, right? I've also tried with pre_option_ on both but it doesn't work and all the other widgets of the sub blog disappears.

    So in the main blogs widget_wp_bannerize column is:

    a:4:{i:2;a:0:{}i:3;a:11:{s:5:"title";s:0:"";s:5:"group";s:5:"right";s:6:"random";s:0:"";s:5:"limit";s:2:"10";s:10:"categories";N;s:16:"container_before";s:4:"<ul>";s:15:"container_after";s:5:"</ul>";s:6:"before";s:10:"<li %alt%>";s:5:"after";s:5:"</li>";s:9:"alt_class";s:3:"alt";s:10:"link_class";s:0:"";}i:4;a:11:{s:5:"title";s:0:"";s:5:"group";s:5:"right";s:6:"random";s:0:"";s:5:"limit";s:2:"10";s:10:"categories";N;s:16:"container_before";s:4:"<ul>";s:15:"container_after";s:5:"</ul>";s:6:"before";s:10:"<li %alt%>";s:5:"after";s:5:"</li>";s:9:"alt_class";s:3:"alt";s:10:"link_class";s:0:"";}s:12:"_multiwidget";i:1;}

    And in the sub blogs widget_wp_bannerize is:

    a:2:{i:2;a:0:{}s:12:"_multiwidget";i:1;}

    I've put the wp_bannerize widget in a dynamic sidebar called 'ad-widget-area' which I'm calling the same way in both the parent and the child theme.

    <?php dynamic_sidebar( 'ad-widget-area' ); ?>

    Should the plugin be activated on the network, separately on each blog or just on the main blog?

    Tried all three however and didn't work either way.

    Any thoughts on what I'm doing wrong? Thanks a lot!

  15. Speggey
    Member
    Posted 3 years ago #

    Ok, so I've got it working with the text-widget but can't get it to work with the plugin. Guess it doesn't fetch the banners from the plugin correctly cause the options is there, echoed them with

    <pre><?php print_r(get_blog_option('4','widget_wp_bannerize') );?></pre>

    Did you have any success justinbriggs1?

  16. justinbriggs1
    Member
    Posted 3 years ago #

    I had success with a few of them, but I ran into a similar problem when I tried to use an ad-manager widget across all blogs. Didn't work, not sure why. I ended up just using a text widget for the ads, so that sort of solved my problem.

  17. Speggey
    Member
    Posted 3 years ago #

    So you're using a plugin shortcode in a text widget? and that works ?

  18. top5free
    Member
    Posted 3 years ago #

    I use duplicate themes on my site. The main blog is the original and the sub domains is an altered copy. I do this so that I can take things from my main blog and code them into my sub domains. Right now I use it effectively for the search box but probably would require additional coding for some thing that changes often.

    Not sure if it's right for you but I use cets_blog_defaults plug-in. It allows you to set defaults for new blogs including themes and plug-ins

  19. Speggey
    Member
    Posted 3 years ago #

    Thanks, I figured it out! had to change some things in the plugin..
    get_option() to get_blog_option() to get the correct url's.

    However now that I override sidebars_widgets with

    add_filter( 'pre_option_' . 'sidebars_widgets', 'override_sidebars_widgets' );

    I can't use any widgets on the blogs independently.

    Thought the solution was to use pre_update_option but that didn't work.

    Is it possible to just add the sidebar_widgets without completely overriding?

  20. David Sader
    Member
    Posted 3 years ago #

    Your original post:

    they will exactly reflect the widgets on the main one.

    There are limits to what you can do with a "switch_to_blog" and "pre_option_" filters. These methods break down when there is more complex dependencies on other options or db queries.

    And now:

    Is it possible to just add the sidebar_widgets without completely overriding?

    It would be simplest to hardcode the advertising widget/functions in each theme's sidebar outside the dynamic sidebar code: <?php if(function_exists( 'wp_bannerize' )) wp_bannerize(); ?> While also modifying the plugin to only get_blog_options from blog 1 and adding the admin menu to blog 1.

  21. Speggey
    Member
    Posted 3 years ago #

    Hi, yeah I guess that would be the easiest. It doesn't work though. The function doesn't exist. I activate the plugin on blog 1, and switch to blog 1 then try to run the function.

Topic Closed

This topic has been closed to new replies.

About this Topic