Don’t let the WordPress address (URL) take the piss out of you!

by Tom on March 17, 2009

I love you WordPress. I hate you WordPress! I want to buy the people behind it a pint and hurt them, oh so badly, all at the same time! It’s quite a mixed sense of emotions :)

I’m not going to start my rant about the use of echo inside functions. I might save that for another blog post! Although feel free to follow me on Twitter for a regular stream of WordPress abuse.

What kicked off this post is we needed to deploy our dev copy of wordpress for a client to look at. The problem being, we want a single ’staging’ db which is hosted remotely that we can use both locally and on the staging server we show the customer. Problem is, the URL of the wordpress is fixed in the DB! Doh. I’m probably missing something here, but I couldn’t find a solution to just automatically pick up on the URL!

Anyway, turns out the solution is pretty simple. Open up wp-config.php and insert the following:

define('WP_HOME', 'http://'.$_SERVER['HTTP_HOST']);
define('WP_SITEURL', 'http://'.$_SERVER['HTTP_HOST']);

This just sets the home url and site url to whatever the current server is. Meaning, you can happily check everything in to SVN and then check it out on to your staging server without having to change any db settings.

I will probably employ a similar trick later to dynamically set the DB config depending on the server.

Love you wordpress!

{ 6 comments… read them below or add one }

Lee Kelleher March 17, 2009 at 5:57 pm

Good tip! I’d remember hearing about that on wp-hackers a while back, (then completely forgot about it).

For the DB config settings, I use a quick-n-dirty if/else …

if ($_SERVER['SERVER_NAME'] == ‘localhost’)
{
// local db settings
}
else
{
// remote db settings
}

Obviously you’d change “localhost” to whatever your local dev hostname would be.

Tom March 17, 2009 at 6:05 pm

Cheers Lee, that was more or less what I was planning to do. Thanks for posting the code… I shall copy and paste that ;)

The other approach I’ve taken before is to setup a local resolver so I can set the DB host to something like MYSQL and then just edit /etc/hosts (or windows/system32/drivers/etc/host on winblows). Also makes migrating the DB server a bit easier.

Lee Kelleher March 20, 2009 at 1:00 pm

You know, I never thought about doing that for the DB host. Using the “hosts” file for that is a good idea. I’ll be doing that in future!

Mitchell July 10, 2009 at 1:34 am

You Legend. 2 seconds of google searching has solved a problem which has messed me around many times!

name July 29, 2009 at 8:45 am

Real,

TriDino February 26, 2010 at 9:42 pm

Thanks, man. I just jerked off my wp site by manipulating the url field.
With your fix, everything works fine again! Thanks a lot!
Chris

Leave a Comment

{ 1 trackback }

Previous post:

Next post: