October 23rd, 2007 at 1:07 pm by David Potter

Multi-blogging

I started using WordPress in September and created quite a few while learning some of the ins and outs of the system.  Then version 2.3 was released, which included a database schema change.  Maintaining all my blogs was beginning to be a real hassle.  Not only are there multiple databases/sets of tables, but I was also having to maintain multiple copies of all the files, including WordPress itself, themes, and plugins.  Something had to be done.

I looked into WordPress-MU, but it had a number of issues - it didn’t have v2.3 features yet and some themes and plugins don’t work with it.  There’s a terrific article on maintaining multiple blogs on the codex which describes and refers to several methods for solving these problems.

Virtual Blogs

The one that I settled on is called virtual blogs by Stephen Rider.  The solution as described by Stephen depends on symbolic links (or junctions in Windows parlance) in the file system to point to a single installation and a replacement for the standard wp-config.php file.  This solution works well when all blogs are on the same domain (e.g. example.com/bob and example.com/joe).  There is code in the files provided by Stephen that says it should work for sites that differ like bob.example.com and joe.example.com, but you can’t do both right out of the box.

Inspiration

One of the trackbacks to Stephen’s post was by Bryan at Xobni who wanted to find an easy way to add additional blogs as sub-folders of the same domain without having to create additional files in the file system.  Between the two posts, I started thinking about how to make this solution work for any domain and any number of sites.

Multi-Blogging To The Next Level

The solution is really quite simple.  Goals of my solution include:

  • The standard multi-blogging goals (single copy of WordPress, themes, and plugins).
  • Limit the number of files that need to be created/modified when adding a new site.
  • Support multiple domains and multiple folders at the same time, all using the same set of files.

Accomplishing this required making changes to the structure of Stephen’s virtual blog code, so since I was in there I changed some of the terminology.  This solution will work equally well using symbol links/junctions or using virtual folders in the web server (IIS in my case).  Here’s what you have to do.

  1. Copy all the WordPress files to the location of your choice.  For me, that was C:\Inetpub\MyWebSites\CommonWordPress.
  2. Download the multi-blogging files from this site and copy them to wp-content\multiblog.
  3. Edit the mb-config-default.php file in that folder to set default values for the database, table prefix, username, and password.
  4. Copy wp-config.php from wp-content\multiblog to the WordPress root (e.g. the CommonWordPress folder).
  5. Edit wp-config.php to describe your sites.  I’ll describe this in more detail shortly.
  6. Create web sites/virtual folders in your web server or create symbolic links/junctions in the file system to point to the WordPress root.  Everything points to the same place.
  7. Create a folder in the wp-content\multiblog folder for each site (e.g. wp-content\multiblog\bob, wp-content\multiblog\joe, etc.) so that each one can have its own file system location for site-specific files (e.g. uploads, downloadable files when using the WP-DownloadManager plugin, etc.).  Note that whenever a plugin needs a location to store site-specific files (usually below wp-content), you will want to use this new location you are creating here instead.
  8. On the Miscellaneous Options admin page, change the uploads folder location from wp-content/uploads to the folder you created in the previous step (e.g. wp-content/multiblog/bob/uploads).
Configuration Files

There are two files that work in tandem: wp-config.php and mb-config-default.php.  Let’s take a look at wp-config.php, which is where the meat of the configuration occurs.  This is the only file you should have to edit when adding new sites once you have this solution configured.

Stephen’s solution used a variable array called $vusers.  This seems to me to make assumptions about how WordPress is being used (i.e. only for blogs written by users) so I changed it to $vsites.  Each entry in the array is a multi-valued string describing the web site, with each value separated by a vertical bar character (|).  Leading and trailing whitespace will be trimmed so you can align the fields in each entry if you so choose.  These are the fields you can specify in each entry.

Field Description Variable
Site name The name of the site that can be displayed in a post or page.  Examples from my site include David’s Technical Blog, DPotter.net, etc. $mb_siteName
URL URL to the root of the site (e.g. dpotter.net/Technical, dpotter.net, etc.). $mb_siteUri
Name of configuration file Defaults to mb-config-default.php, which will typically be what you want.  Defaults for the following fields are used by that file. $mb_configFile
Database Used to set the DB_NAME option in the config file.  The mb-config-default.php file allows you to set a default value (e.g. wordpress). $mb_database.
Table prefix Used to set the $table_prefix variable in the config file.  The mb-config-default.php file allows you to set a default value (e.g. wp_). $mb_tablePrefix
Username Used to set the DB_USER option in the config file for accessing the MySQL database.  The mb-config-default.php file allows you to set a default value. $mb_siteUser
Password Used to set the DB_PASSWORD option in the config file for accessing the MySQL database.  The mb-config-default.php file allows you to set a default value. $mb_sitePw

 

Here is an example of initializing the $vsites array:

$vsites[] = "Bob’s Blog   | users.com/bob | | users  | bob_ | |"
$vsites[] = "Joe’s Blog   | users.com/joe | | users  | joe_ | |"
$vsites[] = "Widget Corp. | widgets.com   | | widget | wp_  | |"

In this example, all sites use the default config file (mb-config-default.php).  They also use the default user and password for accessing the database.  The last entry uses the default table prefix while the first two use the same database so must also set a different table prefix.

One thing you’ll want to make sure of.  If you have a site whose URL is a subset of another site, make sure you place that entry after the other site.

$vsites[] = "Bob’s Blog   | widgets.com/bob | | users  | bob_ | |"
$vsites[] = "Widget Corp. | widgets.com     | | widget | wp_  | |"

Icing

The mb-functions.php file implements three functions, two of which are used during configuration (get_virtual_site_info and mb_config_NF).  There is a third function that is useful for providing a directory to all the blogs hosted by the one common WordPress folder - print_virtualsites_list.  This function can be called from a page or a post (if you’ve got a plugin like Exec-PHP activated) to give visitors to one or more of your sites a list of all the sites hosted by you.

Conclusion

I’m currently in the process of moving all my blogs over to this model.  This has really simplified managing multiple blogs.  Many, many thanks go to Stephen Rider for his solution.

Updated: October 29, 2007

Added a plugin to display various virtual site info using keywords.

Resources

2 Comments

  1. Stephen Rider · October 23rd, 2007 at 3:10 pm #

    You download link doesn’t work.

  2. David Potter · October 24th, 2007 at 12:17 pm #

    Sorry about that. I moved my blog recently and the download manager templates needed tweaking. I tested it and it works now.

Leave a Comment




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