When building websites, there are often times when we need to work on a development server, then deploy to a staging server, then publish to the world. Depending on each server’s configuration, we might have to change the home url and site url variables in the database for the WordPress site every time we move the site to a new environment.

Is there a way to avoid this? Yes!
Add the following code into your wp-config.php file, and WordPress will dynamically change the home url and site url for you.

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

This will disable the “WordPress Address (URL)” and “Site Address (URL)” options in the settings page, so depending on what you want to do, you might want to remove it from wp-config.php too.

Follow me

Sean Wu

I love coding, and I believe I can make your life easier with my coding skills. I also love making cool websites and innovative apps. Oh, did I mention I'm a coffee junkie too?
Follow me

Latest posts by Sean Wu (see all)

Dynamically set WordPress home url and site url
Tagged on:                 

Leave a Reply

Your email address will not be published. Required fields are marked *