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');
This entry was posted in Development, wordpress and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

6 Comments

  1. Posted August 19, 2009 at 10:27 am | Permalink

    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. Posted August 26, 2009 at 11:26 am | Permalink

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

  3. Jason
    Posted August 31, 2009 at 12:57 pm | Permalink

    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
    Posted October 7, 2009 at 6:19 am | Permalink

    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
    Posted November 4, 2009 at 10:28 am | Permalink

    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. Posted September 26, 2010 at 4:21 pm | Permalink

    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

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*
  • randy moss combine results
  • tea party zombies download
  • search 78search 800 numbers
  • randy moss bio
  • dis systems
  • archos
  • randy moss yahoo stats
  • roses
  • dis quand reviendras-tu
  • xanadu bengals
  • zara phillips and the queen
  • 4pm cspancspan area 51cspan 90.1
  • freida pinto miral
  • search lsu.edu
  • mtv oddities
  • seagate
  • battleship aurora
  • greg olsen university of miami
  • mtv kings of leon
  • chad ochocinco sisterchad ochocinco twitter
  • tolerance
  • search engines cookiessearch engines definition
  • bonus
  • gears
  • hartmann
  • dis 0 0.9
  • vince young yahoo stats
  • radar
  • beers
  • search 2.0
  • homeless
  • dis boards cruise
  • search engines us
  • chad ochocinco johnson
  • bengals qb situation
  • gregg olsen books
  • zara phillips dating
  • vince young uncle rico gif
  • freida pinto green dress
  • bea luna
  • chicago bears media relations
  • framed
  • la ink 2011 season 5
  • deed
  • battleship vittorio veneto
  • di's hallmark
  • meant
  • chicago bears 61
  • vince young depression
  • cspan hosts
  • ratchet
  • new england patriots gillette stadium
  • chicago bears 08 record
  • chicago bears bleacher report
  • battleship yamato wreck
  • chicago bears training camp
  • la ink corey
  • chicago bears 1985
  • search engines watch
  • search engines questions
  • pendant
  • cspan facebook
  • connecticut renaissance faire
  • battleship egg hunt
  • serenity
  • kilo
  • bangles eternal flame mp3bengals forum
  • prairie
  • afternoon
  • greg olsen vikingsgreg olsen wife
  • springhill
  • tea party birthday
  • vince young quiz
  • bea zuberbühler
  • battleship ipad
  • betty
  • search engines of the world
  • bea 0b0 105
  • dis x
  • chicago bears tattoos
  • greg olsen vancouver
  • hp support 530
  • chicago bears expo
  • quran
  • legend
  • search engines 9
  • 60 search engines virus
  • zara phillips baby
  • freida pinto zac posen
  • connecticut education
  • battleship yamato 2010
  • search vim
  • hp support error 1005
  • pads
  • connecticut post
  • mtv executivesmtv fantasy factory
  • connecticut secretary of state
  • search 990 finder
  • rooster
  • homeowners
  • wein
  • chicago bears 96
  • vince young yahoo stats
  • worst
  • c span yesterdayc span zelaya
  • randy moss jail
  • new england patriots 98.5
  • hp support quick test pro
  • zara phillips baby
  • la ink games online
  • chicago bears pictures
  • chad ochocinco bears
  • zara phillips wedding hat
  • chad ochocinco vs skip bayless
  • chad ochocinco and cheryl burke
  • new england patriots jake locker
  • tijuana
  • hp support englandhp support forum
  • bengals arrests
  • c span video contest
  • mtv dougie
  • new england patriots 80
  • tea party zombies download
  • vince young redskins
  • cspan presidents
  • palma
  • bear gryllsbea hive dance studio
  • zara phillips kids
  • battleship galactica
  • randy moss 98 vikings
  • beagle
  • extending
  • hp support id
  • connecticut 5th district
  • vince young football camp
  • search tumblr
  • la ink cast
  • search 32
  • battleship wilmington nc
  • randy moss 07 08 highlights
  • hose
  • dis pater
  • crimper
  • la ink 03x05
  • mandolin
  • hp support 6500a plus
  • battleship bismarck wreck
  • search engines before google
  • cspan washington correspondents dinner 2011
  • reign
  • chad ochocinco quotes video
  • bea goldfishberg
  • dependency
  • spherical
  • coronado
  • randy moss vikings 2011
  • battleship lexington
  • mtv true life
  • vince young endorsementsvince young foundation
  • hiding
  • search and seizure
  • search engines usage statistics 2010
  • connecticut lottery
  • dressing
  • licensed
  • mtv overdrive
  • tea party nj
  • hp support chat
  • hp support greece
  • connecticut juvenile training schoolconnecticut kids
  • hp support error 1005
  • dis lyrics
  • bea 71 series staples
  • odessey
  • bea test
  • mtv 5 cover
  • new england patriots needs
  • la ink 04x01
  • vince young 99 yard video
  • rotation
  • search operatorssearch people
  • battleship hacked
  • bea verdi
  • zara phillips fascinator