I was recently involved in a project where it was required that each time you add a new blog on WordPress MU, that it automatically assigns a set of default widgets and activates all the plugins. Finding a plugin that activates all plugins on blog creation was easy, there’s one out there called Plugin Commander and it’s awesome – you can also mass activate and deactivate plugins on all your blogs.

After some googling for a widget activation plugin I came across this forum. The people seemed to know the hooks and calls to activate widgets but of course I ran into problems – mainly with widget ids.

Here is the code I had in my theme/functions.php file:

// Can we register sidebars?  Hopefully!
if(function_exists('register_sidebar')){
        // Give your sidebar a unique id and a name, the name will display in the WP control panel
        register_sidebar(array('id' =>'my-sidebar', 'name' => 'My Sidebar'));
}
// We'll want to assign widgets to the sidebar
if(function_exists('register_sidebar_widget')){
        // Give your widget a name and provide a callback function
        register_sidebar_widget('My Calendar', 'widget_my_calendar');
}
// Our callback from the calendar widget above
function widget_my_calendar(){
        echo '<h2>This is my calendar widget being outputted to sidebar.php</h2>';
}

Then I found out that to automatically register these widgets to a sidebar on blog creation, that you need to know their unique widget id. I then had a peek in the database in “wp_options”, browsed by “option_name” and found “widget_my-calendar”. WordPress automatically generated a slug for me based on my widget title “My Calendar”. Now I was kind of iffy about that since it’s obviously better to provide your widget with an id, that way you know how to target it automatically via WordPress’ built-in “update_option” function.

I did some quick searching through the WordPress core files and saw where it was generating the slug. I had then seen that it returns an generated slug (id) along with params from “register_sidebar_widget” to “wp_register_sidebar_widget“… DUH! So the wp_register_sidebar_widget function actually lets you pass your id as the first parameter – awesome.

Enough of my blabbering – here’s the plugin I came up with for auto-activating widgets:

// Put this in wp-content/mu-plugins/auto-widget-activate.php
/*
Plugin Name: Auto-assign widgets to blog
Version: 1.0
Plugin URI: http://boedesign.com/2009/08/15/auto-activate-widgets-in-wordpress-mu
Author: Jordan Boesch
Author URI: http://boedesign.com
Description: When you create a new blog on MU, have it automatically assign widgets.
*/
 
define('WIDGET_INFO', serialize(array(
    'sidebar' => array('id' => 'my-sidebar', 'name' => 'My Sidebar'),
    'widgets' => array(
        'my-subscribe',
        'my-search',
        'my-calendar',
        'my-poll',
        'my-recent-comments',
        'my-more-blogs',
        'my-categories'
    )
)));
 
// Everytime you create a new blog, enable all the widgets.
function add_default_widgets($blog_id)  {
 
    $widget_info = unserialize(WIDGET_INFO);
 
    // We are simply putting the widgets into the table when a blog is created
    foreach($widget_info['widgets'] as $widget_id){
        update_option('widget_' . $widget_id, array());
    }
 
    // Tell what sidebar to have what widgets
    update_option('sidebars_widgets', array($widget_info['sidebar']['id'] => $widget_info['widgets']));
 
    return;
 
}
 
// Call the populate_options action on blog creation
add_action('populate_options', 'add_default_widgets');

Now that you’ve inserted them into the database on blog creation, you need to provide their callbacks and output.
Put the following code in the functions.php file (theme directory).

// All widget info is set in wp-content/mu-plugins/assign_widgets.php
$widget_info = unserialize(WIDGET_INFO);
 
// This needs to be called to tell WP that we want to use widgets for this theme
if(function_exists('register_sidebar')){
   register_sidebar(array('id' => $widget_info['sidebar']['id'], 'name' => $widget_info['sidebar']['name']));
}
 
// Our own custom widgets with callbacks
if(function_exists('wp_register_sidebar_widget')){
 
    foreach($widget_info['widgets'] as $widget_id){
        // first param is our widget id, second param is a humanized version of the widget id (My Calendar) and the third param is the function that we call to display the widget
        wp_register_sidebar_widget($widget_id, ucwords(str_replace('-', ' ', $widget_id)), 'widget_' . str_replace('-', '_', $widget_id));
    }
 
}
 
// Now you need to create a function for each widget that you assigned in that array, remember "my-calendar", "my-subscribe" etc?  Those functions will look like this now:
function widget_my_calendar(){
        echo "<h2>I'm a calendar widget!</h2>";
}
 
function widget_my_subscribe(){
        echo "<h2>I'm a subscribe widget!</h2>";
}

Now put this code in sidebar.php (theme directory) to call your sidebar.

dynamic_sidebar($widget_info['sidebar']['id']);

Happy Mu-ing!