WordPress Snippet: Override wordpress url and site url for easy development and production environment
While I hope to figure out a truly seamless method of maintaining a development and production environment for WordPress this is one of the things I do to streamline my WordPress development.
The problem is if you want to update you local database with the production database (or vice-versa) there is a whole rigamarole of updating url address paths stored in the WordPress database requiring searching and replacing a mysql dump, using a search and replace regex plugin, and other wackiness. The biggest issue is the url for the WordPress install itself and the Site URL (set during install and managed in SETTINGS > GENERAL of the WordPress admin). These paths are used anywhere WordPress dynamically outputs a url on your blog. RSS feeds, permalinks, rewrite rules, menus, and the list goes on.
However, you can easily override the WP install url and the site url in PHP before WordPress loads the options.
wp-config.php
...
// Switch for various development/staging/production environments
// Override certain admin settings and customize environment for development staging
switch ($_SERVER['HTTP_HOST']) {
// development
case 'beingzoe.dev':
define("WP_HOME","http://beingzoe.dev"); // no trailing slash
define("WP_SITEURL","http://beingzoe.dev/wordpress"); // no trailing slash
define('WP_DEBUG', TRUE); // comment out where this is normally defined earlier in the file
break;
// production
default:
define("WP_HOME","http://beingzoe.com"); // no trailing slash
define("WP_SITEURL","http://beingzoe.com/wordpress"); // no trailing slash
define('WP_DEBUG', FALSE); // comment out where this is normally defined earlier in the file
break;
}
...
The codex offers another solution, but I a SWITCH makes more sense to me than an IF and then as you might notice in the example I also automatically turn on debugging on my local development server. In this way you can customize your various site server environments running on WordPress in a number of ways.
I usually put this code near the end of wp-config.php
just before the settings are included
Perhaps unintuitively after the comment saying not to do such things ;)
...
/* That's all, stop editing! Happy blogging. */
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
...development/production switch code goes here...
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
...
EOF
Admittedly this does not address a myriad other issues in having a seamless deployment workflow for WordPress from development to staging to production. But it is one of many things I do to keep things simple.
Bonus snippet
If you also want to test multiple WordPress databases on one install/theme this is also easily accomplished taking advantage of the settings in wp-config.php. Just add new $table_prefix to your settings (and comment the others ;).
$table_prefix = 'some_test_data_'; //$table_prefix = 'my_real_wp_prefix_';
This simply triggers a “new install” of WordPress in the same database. Then you can easily switch between multiple “WordPresses” for extreme acid testing.
Unsolicited nag
And because I always have debug mode on in my development server, I would love it if you at least checked your WordPress plugin for E_NOTICES once before uploading it. :)

