One-button website publishing using WordPress-mu

I’m kinda loving on WordPress MU. One of my recent projects required building web sites that pulled from a shared database. WordPress MU allowed me to create a one-button website builder for my client. WordPress MU calls them blogs, but my client will map a unique domain to each blog, and, well, doesn’t use them as blogs at all. So I’m calling them sites here.

By filling in three fields and clicking a button, my client creates a website that

  • Associates metadata with the site that specifies filter criteria to select records from the shared database
  • Sets the permalink structure for the new site to a custom setting
  • Creates a key category for the new site, one that is used for the posts generated in a later step
  • Sets the theme
  • All the initial pages are created, including content. The “slug” is set specifically to support the URL structure we want. Page template is also set here because our design calls for a hierarchy of pages.
  • If the PageMash plugin is active (it is auto-activated for all new blogs using Plugin Commander), certain pages are hidden, and a specific order is set so that page navigation comes out looking good
  • The front page is set, because we are creating CMS sites, not blogs
  • Several hundred posts are generated out of the underlying shared non-WPMU database tables

All of this takes something like 20 seconds, at which point the admin can visit the new site as a subdomain.  The theme has been applied, navigation works correctly, it is beauty.

Here’s how it is done in code.  All you need to do is register a function for the action hook that WordPress MU runs when a new blog is created.

function mypp_initialize_blog($blog_id) {
  global $wp_rewrite;
 
  // first, switch to the new blog; we will undo this at the
  // end of the function with restore_current_blog()
  switch_to_blog($blog_id);
  // add a blog option, here a filter with a default value
  add_option($blog_id, 'campus_selection_criteria', "CampusStateID='CA'");
  // Set a custom permalink structure
  $wp_rewrite->set_permalink_structure('/%category%/%postname%');
  $wp_rewrite->flush_rules();
  if (function_exists('wp_create_category')) {
      wp_create_category('schools');
  }
 
  // switch theme - this one specifies a child theme
  switch_theme('corporate', 'tweaked-corporate');
  // Prepare options used by the pageMash plugin, auto-activated for new sites
  if(!is_array(get_option('exclude_pages')))
    $excludePagesList=array();
  else
    $excludePagesList = get_option('exclude_pages'); //if it's empty set as an empty array
  // add pages to support the basic page structure
  // NOTE: the full code is not shown, standard use of wp_insert post
  // for each page:
  $newid = wp_insert_post($postdata);
  if ($newid && !is_wp_error($newid)) {
    add_meta($newid);
    if ( IWANTOEXCLUDETHISPARTICULARPAGEFROMNAVIGATION )
      $excludePagesList[] = $newid;
    if (THISPARTICULARPAGE == 'home') {
      update_option('show_on_front', 'page');
      update_option('page_on_front', $newid);
    }
  } else {
    // error handling if insert post failed
  }
  // end for each page
  update_option('exclude_pages', $excludePagesList);
  // now generate posts for schools from the shared database
  createSchoolAsPost('', $blog_id);
  // we're done! restore our initial blog
  restore_current_blog();
} // function mypp_initialize_blog
 
add_action('wpmu_new_blog', 'mypp_initialize_blog');
Posted in Development, wordpress, wordpress-mu Tagged with: , , , , ,
3 comments on “One-button website publishing using WordPress-mu
  1. Fiona says:

    Hi Greg,
    Excellent post! I’m doing something similar for ‘instant’ partially populated websites using WPMU. I’ve been making edits to wpmu-functions.php to create the initial pages but was having serious bother with hierarchy and child pages.

    I really like what you’ve done and I am going to give your method a try instead.

    Fiona

  2. Hi Greg,
    Even am working on a similar project. Am researching on how to use the e-commerce plugin so that the client can register and after registering, he can buy any particular theme, and then use it. Do you have any particular suggestions?

    Thank you.

  3. Arun George says:

    Heyy,

    Thankz a lot.. excellent blog.
    Currently i am working on a Multi Blog API project. Here i got stuck on the portion of category creation for sub blogs. A small function from your post ‘switch_to_blog();’ helped me a Lot.. Keep posting.. All the Best..

    Arun G!

Leave a Reply

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

*