How to give all new WPMU blogs default properties, theme and pages

I’ve been developing an application on WordPress-MU and thought I’d share a cool tip.

When new web sites are generated by my client, I wanted the experience to be painless; a one-button ready-to-go web site.

It turns out there is an action hook (of course there is!) when a new blog is created.  So let’s get down to the nitty gritty and see how to implement it.

I wanted to create a property that would apply to the new web site, which would allow my client to limit what data gets displayed on that site.  I also wanted to generate the basic set of pages that I am using to support the sites’ URL structure.  Each page has a unique template that gives it some special navigation and content mojo.

So first I set the site-global option:

add_blog_option($blog_id, 
	        'campus_selection_criteria', 
                "CampusStateID LIKE '%'");

Now for the pages with their templates.  In order to let wp_insert_post know which blog to use, we use a handy function call:

switch_to_blog($blog_id);

We will also need to specify the theme we are using so that the starting theme will be selected and the templates will be correctly associated.

I can then create the pages as I would in WordPress-non-MU.  The final step is to call add_action() to hook this code into the blog creation event.

Here is the code in its entirety

function dscp_initialize_blog($blog_id) {
   add_blog_option($blog_id, 
                   'campus_selection_criteria', 
                   "CampusStateID LIKE '%'");
   switch_to_blog($blog_id);
   // switch theme
   switch_theme('thematic', 'mychildtheme');
   $postdata = array('post_parent' => 0,
   'post_status' => 'publish',
   'post_title'   => 'DESCRIPTIVE TITLE',
   'post_name'  => 'descriptive-title', /* the slug */
   'page_template' => 'template-whatever.php',
   'post_type'   => 'page');
   $newid = wp_insert_post($postdata);
   if ($newid && !is_wp_error($newid)) {
      add_meta($newid);
   } else {
      // your error handling code
   }
}
 
add_action('wpmu_new_blog', 'dscp_initialize_blog');
Posted in Development, wordpress Tagged with: , ,
6 comments on “How to give all new WPMU blogs default properties, theme and pages
  1. Joel Adria says:

    Hi there,

    This is just what I’ve been looking for! Where did you put this code exactly though? If it were placed in the functions.php file it wouldn’t be called if that theme wasn’t in use… can you just place it anywhere? Like wp-config.php or something?

    Thanks,
    -Joel

  2. @Joel: I wrote a plugin that has to be activated for the functionality to work.

  3. Jason says:

    Cool, this will really help! I’m just a little confused about what add_blog_option($blog_id,
    ‘campus_selection_criteria’,
    “CampusStateID LIKE ‘%'”);
    accomplishes?

  4. Padma says:

    As I am newbie I don’t know how to write plugin for a function to work ,Can you give me some reference for that function to work ?

  5. Ninjaboy says:

    Wow Gregory – you have just saved me so much work! Thank you for sharing this, just what I have spent a very long time researching… works a treat!

  6. aaron lamar says:

    Hey just so you know the

    ‘page_template’ => ‘template-whatever.php’,

    is no longer used as of 10/1/2009

    in stead you have to update the meta data using

    here is a the wordpress page to learn how to use it

    http://codex.wordpress.org/Function_Reference/update_post_meta

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

My sites

My Github Profile