During site development, there are hundreds of small ways to improve efficiency and ease of use. As it pertains to WordPress, you can find a lot of room for optimization, flexibility, and efficiency in your wp-config. More specifically, by defining constants, you can take care of some cumbersome tasks or issues.
One of the ways you can leverage wp-config is to use the WP_SITEURL and WP_HOME constants to your benefit. There are a countless online posts that list these constants as things that can be modified/set in wp-config, but it’s worthwhile to spend some time addressing how to use them and why they are useful.
What to Know Before You Start
The first thing to figure out is what these constants actually do. WP_SITEURL and WP_HOME override the “siteurl” and “home” options in the wp_options database table. These options control the site’s understanding of where the WordPress core files and actual home page reside, respectively. In most cases, these values will be the same, but there are some cases where these values will be different (if the WordPress core files are stored in a non-root directory, for instance). These values are stored in the database and can be modified via the WordPress dashboard, but not without potential pitfalls.
The chief danger of changing these values via the dashboard is that making a mistake with the site URL can prevent you from accessing your dashboard, thereby forcing you to change this value directly in the database.
Furthermore, modifying the values in the dashboard makes moving the site between staging URLs and environments cumbersome because, when the site is being moved between environments, these values need to be modified in the database. While there are many tools/plugins that can help with migrating and moving sites/databases between environments, this article aims to educate about the changes that need be made behind the scenes.
How Defining WP_SITEURL and WP_HOME Can Help You
Defining these constants in wp-config overrides the database values, which can help in a variety of ways:
- If your site becomes inaccessible because a value was set incorrectly from the dashboard, you can regain access to the site and the dashboard by overriding the incorrect value in wp-config.
- You can modify these values to match the URLs of staging environments and production environment on an individual install basis without needing to change the database values every time.
- You can use PHP to declare these values dynamically, making your wp-config file portable between environments.
Defining the Constants
It’s fairly simple to define constants. We’ll look at two ways that constants can be defined.
The first (and most basic type) is the static/predefined URL. This constant should be defined if you’ve made a mistake when editing it from the dashboard or if you would like to override the database values. In the following code, I’ve defined the static/predefined URL constant for pressable-com-clone.mystagingwebsite.com:
Definition really shines when you use PHP’s $_SERVER global variable to populate these values dynamically:
When the $_SERVER global variable is set to populate the constants dynamically, WordPress will use whatever hostname/domain the virtual host passes along to the directory.
With the WP_SITEURL and WP_HOME constants updated dynamically, your WordPress installation is portable to any URL. This means you can more easily migrate your site to a new host, stage a site, view an archived site, or take a development site live.