Assigning default sidebar widgets

I want to assign default sidebar widgets for all new blogs. I am able to assign sidebar widgets to the left, center and right sidebars using a new file I called sidebarwidgets.php in the wp-content/mu-plugins directory. The code for in sidebarwidgets.php is:

<?php

function new_blogs_setting( $blog_id ) {

add_option( 'widget_categories',
array( 'title' => 'My Categories' ));

add_option("sidebars_widgets",
array("sidebar-1" => array("recent-posts"),
"sidebar-2" => array("categories", "tag_cloud"),
"sidebar-3" => array("pages", "links")));

}
add_action('populate_options', 'new_blogs_setting');

?>

This works great and the setting is applied to all NEW blogs created after uploading sidebarwidgets.php. However, I can’t figure out how to create a default setting for the blog sidebar (sidebar-4). When I put in code with a similar syntax pointing to the unused "archives" widget it causes the whole site to white screen. In the interim, users see a message asking them to add a widget to the blog sidebar, so they would have to add that manually.

I am no programmer, so I am hoping someone wiser than I might be able to point me in the right direction. This be a great plug-in with a little modification. For now, I'm just frustrated. Thanks!

    drmike

    I'm not in a position to check but are we 100% sure it's called 'archives'? I seem to remember something about that one having a funky name. Would be surprised that it would cause a white screen. Also anything in the webserver's error logs?

    col235
    • 18
    • #6,547

    drmike, thanks for your reply! The error turned out to be a simple coding error that I was too blind to see. All fixed now.

    This works great and the setting is applied to all NEW blogs created after uploading new file sidebarwidgets.php to the wp-content/mu-plugins directory

    Here is the corrected code for sidebarwidgets.php in case anyone else needs it:

    <?php

    function new_blogs_setting( $blog_id ) {

    add_option( 'widget_categories',
    array( 'title' => 'My Categories' ));

    add_option("sidebars_widgets",
    array("sidebar-1" => array("categories"),
    "sidebar-2" => array("tag_cloud"),
    "sidebar-3" => array("pages", "links"),
    "sidebar-4" => array("archives")));

    }
    add_action('populate_options', 'new_blogs_setting');

    ?>

    Kirk Ward

    @col235,
    @DrMike,
    @anyone else who can answer ...

    Will this code still work with the WPMU ver 2.9.2 I just installed to allow me to define and require a widget in all new blogs? If it does, since I am no coder, can someone explain what the code is doing? Is it installing a widget of the name shown in the array parameter in the sidebar with the id shown, as in sidebar-2 being the sidebar with id #2?

    I am wanting to solve the problem I described in http://premium.wpmudev.org/forums/topic/want-advert-to-appear-in-sidebar-on-all-blogs-how

    Thanks,
    Kirk

    Andrew
    • Erstwhile founder
    • 7,430
    • #14

    Hiya,

    This won't work for what you want to do as the users would just be able to remove the widget.

    Thanks,
    Andrew

    Mason

    I was just looking for something like this for a new project. Knew it was possible! Thanks for sharing!

    Does anyone know if this is possible to do on a "per theme" basis rather than globally? The site I'm building will have two themes for new users to choose from. Depending on the theme, I'd like different widgets to come up as the default. Is there someway to add this to the functions.php part of a theme rather than as an mu plugin? Thanks!

    drmike

    Does anyone know if this is possible to do on a "per theme" basis rather than globally?

    Yes, hard code it into the theme's sidebar.php. :)

    Kirk Ward

    I called sidebarwidgets.php in the wp-content/mu-plugins directory.

    This won't work for what you want to do as the users would just be able to remove the widget.

    Yes, hard code it into the theme's sidebar.php. :)

    I need a bit of eddication here.

    I was accepting that if the code was called from the mu-plugins directory, that future widget settings would override it, but what do happen if it is in the sidebar.php? Would that be overwritten by later settings?

    drmike

    If you want default "widget" in your sidebar based on what theme you;re using, I would just modify the themes' sidebar.php files as they already have "widget" output in there.

    Think pre-widget wordpress and the old way they did sidebars before widgets came out. You hardcoded the sidebar.php files to get things to display in the sidebars.

    Kirk Ward

    I was wanting the bloggers to start with a pre-set bunch of widgets that they could remove and replace as needed. It seems my target audience needs a bit of show and tell as they seem to get lost in their admin panels.

    That being said, with two sidebars (l_sidebar.php and r_sidebar.php) in their initial (or new blog default) theme, would they use the method the OP described?

    Kirk Ward

    Hi,

    I tried the corrected code given above and ran into a problem. I recieved PHP error messages that headers had already been sent when a plugin tried to register a session. Any ideas about what to do to correct that?

    Mason

    The problem I'm having with adding the widgets directly to the sidebar in the theme is that my themes are child themes of a theme framework. Some of the widgets and sidebars I'm using are called from the parent theme/framework while others are custom ones I've put into the child theme.

    I'm registering the sidebar areas in the functions.php of the child theme. Can I also register the widgets in this file?

    bdonaghy

    We used this code and after upgrading to wordpress 3.0.1 it doesn't work anymore. Anything like this out there or any adjustments I can make for wp 3.0.1?

    Debbie
    • Monthly
    • 14
    • #8,093

    I was about to ask the exact same question, too bad wordpress 3.0.1 doesn't support it anymore. Does anyone have a thought on this? Possible hack to make it work in the new version?

    I would really like to have default widgets activated in new user's blogs. Some of my pages just don't make sense without them.

    (And also, very off topic: hi all! Bit rude to use my first post on this without introducing myself. So here goes: Hi, I'm Debbie. I'm new. I baked cookies for you all. Share equally.)

    drmike

    Greets:

    Actually we run on dark chocolate around here. At least that's the in joke. Used to be steak dinners but that was on a different support forum a long time ago.

    Sue
    • Staff
    • 662
    • #128

    Hi Debbie

    Firstly welcome to WPMU DEV.

    Cookie should work for some of us (unbelievable but some of the members of the team don't like chocolate!). But definitely dark chocolate won't work Dr Mike. All types of chocolate, other than dark chocolate and bacon chocolate, are acceptable.

    Now back on topic :) Debbie, have you checked out our New Blogs Template - http://premium.wpmudev.org/project/new-blog-template That is a really easy way to preset so all newly created sites have the same widgets. You can use it to inherit all widgets, posts, pages, categories, theme settings and more from a template you have created.

    If you scroll down on the installation page you will see detailed instructions on how it is used - http://premium.wpmudev.org/project/new-blog-template/installation/

    Debbie
    • Monthly
    • 14
    • #8,093

    Hi Sue!

    Well, do I have good news for you on the cookies, as they were all chocolate chip flavored. Eat just the cookie if you like cookies, pick the chocolate bits off of them if you like chocolate and hand the cookie over to the cookie lovers. What a win-win situation! ;)

    Thanks for recommending the New Blogs Template plugin to me, at first glance it looks very usefull. Might just be what I needed, I will install it and try it out. I'm curious if these blog templates could also be set as the default template every blog gets on signing up. If that is the case, it totally rocks. Will post my results back to you guys :)

    Best,
    Debbie

    Sue
    • Staff
    • 662
    • #128

    Hi Debbie, you've got the winning combination since the cookies include chocolate chip! Total win situation.

    Yes with the New Blogs Template you can set it up so that one of the templates you have created is used as the default for all new blog sign ups. If you scroll down the installation page it explains how to do this.

    Debbie
    • Monthly
    • 14
    • #8,093

    Excellent! Thanks again, Sue. I tried it with the most advanced settings, pages, posts and tables set up by plugins I could think of and it all got copied extremely well to a new blog created on my homepage.

    You guys truly amaze me. SO worth the cookies.

    Sue
    • Staff
    • 662
    • #128

    Hi Debbie, that is excellent news and we are always happy to assist! Off course you are now thinking Swiss chocolates right?

    Debbie
    • Monthly
    • 14
    • #8,093

    Wrapping them in little heart patterned gift wrapping paper as we speak, Sue.

    Toby Wilson
    • 1
    • #29,655

    Hi,
    I have an issue. I have set the widgets on my default template, but when i create a new blogs then the default blog widgets didn't copied to the new blog. its give an error like the following

    Warning: array_key_exists() expects parameter 2 to be array, boolean given in C:\wamp\www\libertytaxtrax\wp-includes\widgets.php on line 858

    Also the default blog shows their own default theme widgets. Can anyone help me how to fix it?

    Mason

    Hiya myseopartner,

    First off, welcome to WPMU DEV!

    This thread is quite old and a bit long at this point. Please open a new thread and post your question there and someone from our team will respond right away.

    When you create new thread could you also tell us which version of New Blog Templates and WordPress you are running (and BuddyPress is applicable). This way we'll be able to verify that kind of information right from the start.

    Thanks and again, welcome!