Install Wordpress in SVN using svn:externals

Note: This post requires working knowledge of SVN

Goals

  • Keep a complete Wordpress install in your SVN
  • Have the Wordpress core files checked out from their SVN
  • Have as many plugins as possible checked out from their SVN
  • Still provide versioning of your custom themes/plugins

Other Methods

  • Copying/exporting the Wordpress files into your SVN
    • Upgrading requires manually copying all the files over again
  • Checkout a copy of Wordpress
    • Directory will be a working copy of their SVN, any changes you make can’t be versioned in your SVN

Benefits

  • Easy updates, change one line and run a command to download all the changes from a new version
  • Allows you to keep your own themes or plugins in your SVN
  • Easy to copy the same Wordpress install on multiple computers
  • If you need to hack core Wordpress/plugin files, any new changes from new versions will be merged, or provide you with an opportunity to resolve any conflicts.
    • Note that changes to these files cannot be committed to your SVN, however this should encourage you to not hack core files

Setup Directories


Get Wordpress

  • Add svn:externals entry for the core of Wordpress
  • Download the latest version of Wordpress by running svn up

Setup wp-content

Since we want to keep our theme, plugins, and uploads under SVN, we need to make a new wp-content directory outside of core. Export the current directory from core by running:

  • svn export core/wp-content wp-content
    • This will copy over all the files except the .svn directories used by the Wordpress repository
  • Then add the directory and commit it to SVN
    • svn add wp-content
    • svn ci wp-content/ -m "Add wp-content"

wp-config.php

Next step is to copy our wp-config.php outside of the core directory

  • cp core/wp-config-sample.php wp-config.php
  • Add and commit the file to SVN before we make any changes
    • svn add wp-config.php
    • svn ci wp-config.php -m "Add default wp-config.php"
  • Edit the file as usual with proper database settings or other options
  • Add the following lines above the require_once(ABSPATH . 'wp-settings.php'); line:
    • define('WP_CONTENT_DIR', realpath(ABSPATH.'../wp-content/'));
    • define('WP_CONTENT_URL', 'http://www.example.com/blog/wp-content');
    • define('WP_HOME', 'http://www.example.com/blog');
  • Commit the changes: svn ci wp-config.php -m "Add database and wp-content settings"

.htaccess

Create an .htaccess file in the blog directory with the following:

# BEGIN WordPress
<IfModule mod_rewrite.c>
    RewriteEngine On
    # Base is the URL path of the home directory
    RewriteBase /blog/
    RewriteRule ^$ core/index.php [L]
    # Skip real files and directories
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Otherwise send it to WordPress
    RewriteRule .* core/index.php [L]
</IfModule>
# END WordPress

To prevent Wordpress from overwriting this file, change its permissions by running chmod u-w .htaccess

Then add and commit it to SVN

svn add .htacess
svn ci .htaccess -m "Add .htaccess"

Setup Wordpress

Go to http://www.example.com/blog/ and walk through the standard Wordpress install process.

Install Plugins

Plugins can also be installed using the svn:externals method. Many of the common Wordpress plugins have a SVN repo at http://plugins.svn.wordpress.org/. As an example, lets install the All in One SEO Pack:

  • Find and click all-in-one-seo-pack in the list from the above link
  • You have two options, you can select trunk to always have the latest version of the plugin, or go under tags and click the latest version at the bottom. Copy the URL.
  • Navigate into the plugins folder in your custom wp-content
    • cd wp-content/plugins/
  • Add the svn:externals entry for the plugin:
  • Commit the new property and run update to download the plugin
    • svn ci -m "Add all-in-one-seo-pack"
    • svn up

Repeat the process for any other plugins


Updating Wordpress or Plugins

When a new version of Wordpress or a plugin is released, updating is very easy. If you used the trunk URL, running svn up will download all of the changes. If you used tag URL’s, svn up will download all the updates only after changing svn:externals to the latest version.

  • From the blog root: svn pe svn:externals .
  • Change the version number after tags to the latest release
  • Save and commit the property: svn ci -m "Update Wordpress to #.#.#"
  • Get the updates: svn up

Other Resources