wp-config.php constants vs database options for WP_HOME and WP_SITEURL across local and production
0 reputation · 26 Jan 2021, 04:18 UTC
0 reputation · 26 Jan 2021, 04:18 UTC
I'm setting up a WordPress deployment where the same codebase runs locally (e.g., http://mysite.test) and in production (https://mysite.com). The documented options for defining the site address are the WP_HOME and WP_SITEURL constants in wp-config.php, or the home and siteurl rows in the wp_options table.
Each approach has a documented trade-off. Constants override the database values and prevent editing them in Settings → General, which avoids accidental changes but hardcodes environment-specific URLs into versioned files. Database values keep wp-config.php environment-neutral, but a database copied from production to local (or vice versa) carries the wrong domain and can trigger redirect loops or broken asset URLs.
Assume WordPress 6.x on Apache or Nginx, with the database occasionally synced between environments.
wp search-replace or URL-rewriting plugins behave during migrations?35275 reputation · 26 Jan 2021, 11:50 UTC
Define WP_HOME and WP_SITEURL as constants in wp-config.php. This is the only approach that survives database syncs between local (http://mysite.test) and production (https://mysite.com) without manual edits. Constants override the database at runtime, so the wp_options table can be freely copied between environments — the stale home and siteurl rows are ignored by core.
Keep wp-config.php in version control and load URLs from a .env file (excluded from VCS). A documented, widely used pattern:
// wp-config.php (early, before wp-settings.php)
if (file_exists(__DIR__ . '/.env')) {
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
}
$home = $_ENV['WP_HOME'] ?? 'http://' . $_SERVER['HTTP_HOST'];
$site = $_ENV['WP_SITEURL'] ?? $home;
define('WP_HOME', $home);
define('WP_SITEURL', $site);Provide .env.local and .env.production (or .env per server) with:
# .env.local
WP_HOME=http://mysite.test
WP_SITEURL=http://mysite.test
# .env.production
WP_HOME=https://mysite.com
WP_SITEURL=https://mysite.comThis requires vlucas/phpdotenv (install via Composer). The constants must be defined before require_once ABSPATH . 'wp-settings.php'; — late definitions (e.g., in mu-plugins) are ignored for these two constants.
Defining constants does not change wp search-replace or URL-rewriting plugin behavior:
wp search-replace operates only on the database (wp_options, post_content, postmeta, etc.). It never touches PHP constants.wp search-replace 'https://mysite.com' 'http://mysite.test' --dry-run to preview changes; constants remain untouched and continue serving the correct environment URL.WP_SITEURL causes core to fall back to the database for home, often triggering redirect loops.home_url() and site_url() in themes/plugins. Some code reads get_option('home') directly — that returns the stale database value. Search your codebase for get_option('home') and replace with home_url().WP_HOME/WP_SITEURL constants are not supported for network sites. This guidance applies to single-site installs only..env-backed constants to wp-config.php./wp-admin load without redirect loops.wp option get home and wp option get siteurl — they show database values, but a test plugin calling home_url() returns the constant value.Are you on a managed host that may already define these constants at the server level? If yes, share the host name — the recommendation may shift to using their provided mechanism instead of a custom .env approach.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.