A programmer’s setup for WordPress

May 8th, 2009 in Micro Blog

I maintain a few dozen sites using WordPress on a handful of different servers, and have come up with a simple way of setting it up that makes it easy to store in Subversion, and easy to keep synchronized with both my updates and WordPress’s.

The process is simple:

  • Use Subversion svn:externals to link to the official tagged versions of WordPress
  • Separate the custom portions into their own folders, outside of WordPress’s structure
  • Symlink the custom portions into the WordPress setup

Project layout

This layout makes it easy to work with your site, as all of the important things are in one place. It also makes it easier to update the WordPress installation, and to reset it if you accidentally bugger something up.

configure           // A script to symlink resources/ bits to public/
public/             // Linked to WordPress svn (using svn:externals)
resources/
    htaccess/        // The rewrite rules for the site (not hidden)
    images/          // Site images
        uploads/     // WordPress uploads (linking wp-content/uploads)
    js/              // Site JavaScripts
    plugins/         // WordPress plugins
    theme/           // Site theme (can be named too)
    wp-config.php    // Site configuration

The configure script:

ROOT=public

# clean up environment
rm -fr $ROOT/license.txt $ROOT/readme.html $ROOT/wp-config-sample.php

# link resources to web root and CI setup

rm -f $ROOT/images
ln -s ../resources/images $ROOT/images

rm -f $ROOT/js
ln -s ../resources/js $ROOT/js

# ttmys is the name of this site's theme
rm -f $ROOT/wp-content/themes/ttmys
ln -s ../../../resources/theme $ROOT/wp-content/themes/ttmys

rm -f $ROOT/.htaccess
ln -s ../resources/htaccess $ROOT/.htaccess

rm -f $ROOT/wp-config.php
ln -s ../resources/wp-config.php $ROOT/

rm -fr $ROOT/wp-content/plugins
ln -s ../../resources/plugins $ROOT/wp-content/

rm -fr $ROOT/wp-content/uploads
ln -s ../../resources/images $ROOT/wp-content/uploads

Bootstraping the setup

  1. Set up and check the above into your Subversion repository.
  2. Check out the tree on your webserver (making sure Apache is pointed at the public/).
  3. Adjust permissions based on your server setup.

Upgrading WordPress

  1. Edit the svn:externals tag for public, and point it at the new version of WordPress.
  2. Move the old public folder to a backup location.
  3. Update your local working copy (svn up), which will pull in a new version of WordPress.
  4. Rerun configure.
  5. Test your plugins and theme.

Doing more

You can take the approach further, too. For a few of my sites I maintain a static version of the site that’s automatically swapped in when an upgrade is taking place. I also use this layout for projects that weave WordPress into another web application (handy for adding documentation to a site, for example). Keeping the custom files in one place and treating WordPress as a library makes it easier to maintain and extend over time.