Self-Updating Code in WordPress

One of the problems I face when having to deploy new code changes — especially those that require database changes, such as updating options, activating or de-activating plugins, etc — is the lag between deploying the code and making the relevant changes to the database. We can’t really risk the site looking broken for even the shortest time, especially true if the broken page gets cached.

Enter the need for self-updating code. The code will “activate” itself the first time it’s run — no temporary glitches in viewing the website because code is live but db changes aren’t or vise-versa.

The basic concept is simple. You define a “current database version” and a “database version” then compare the two. If they differ, you do stuff.

The code below is an example based on what I use. Feel free to modify it as you see fit to suite your needs. It should go towards the top of your theme’s functions.php file so that it gets executed quickly. See the code comments for more info.


 * Run various updates based on the current "version" of the site DB. The DB
 * version is an arbitrary distinction that has no direct relation to SVN
 * revisions nor tags.
 * @since 2010-04-20 Gabriel Koen
 * @version 2010-04-22 Gabriel Koen
 * @param int $thisDbVersion
 * @param int $dbVersion
// $thisDbVersion is whole numbers to make it easier to calculate and compare.
$thisDbVersion = 2;
// Get $dbVersion from the database, the option name here is proprietary for
// MMC (my employer). Just don't use "db_version" as it will conflict with
// WordPress. The default is set to 0 -- again, to make it easier to calculate.
$dbVersion = get_option('mmc_db_version', 0);
if ( $thisDbVersion > $dbVersion ) {
    // We're doing a do/while loop so that we iterate through each db version
    // incrementally.
    do {
        switch ( $dbVersion ) {
                // Do nothing
            case 1:
                // Run a SQL update script.
                $filename = '/path/to/file.sql';
                exec('mysql -u ' . $_SERVER['DB_USER'] . ' -p ' . $_SERVER['DB_NAME'] . ' < ' . $filename);                 break;             case 2:                 // see my post on automatically activating and deactivating                 // plugins for more info                 mmc_deactivate_plugins('mycategoryorder/mycategoryorder.php'); mmc_activate_plugins(array('mmc_category_order.php', 'mmc_ads.php'));                 break;         }         $dbVersion++;     } while ( $thisDbVersion >= $dbVersion );
// Update db version to current.
if ( $thisDbVersion != $dbVersion )
    update_option('mmc_db_version', $thisDbVersion);

Combined with environment-based settings, this can become very powerful. You can run pseudo build tasks like:

$filename = '/path/to/file.sql';
exec('mysql -u ' . $_SERVER['DB_USER'] . ' -p' . $_SERVER['DB_password'] . ' ' . $_SERVER['DB_NAME'] . ' < ' . $filename);

Related External Links


One thought on “Self-Updating Code in WordPress

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s