Skip to content

Instantly share code, notes, and snippets.

@JARVIS-AI
Created January 15, 2020 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JARVIS-AI/3d30bf421eb919f31573a230fad768bd to your computer and use it in GitHub Desktop.
Save JARVIS-AI/3d30bf421eb919f31573a230fad768bd to your computer and use it in GitHub Desktop.
Set WordPress site URL in the config file instead of the database
<?php
// WordPress stores the site URL in the database by default (which I have never
// understood), and it's a pain to have to type out the UPDATE SQL or search in
// phpMyAdmin to change it. This is a simple way to put the URL into
// wp-config.php instead.
// Note that you will still need to update any URLs that appear in the content,
// especially when you copy a database from a development site to production:
// https://gist.github.com/davejamesmiller/a8733a3fbb17e0ff0fb5
//----------------------------------------
// Add these lines to wp-config.php:
define('WP_SITEURL', 'https://www.domain.com');
define('WP_HOME', 'https://www.domain.com');
//----------------------------------------
// Or add appropriate logic so it works on all servers:
if ($_SERVER['HTTP_HOST'] == 'dev.domain.com') {
define('WP_SITEURL', 'http://dev.domain.com');
define('WP_HOME', 'http://dev.domain.com');
} else {
define('WP_SITEURL', 'https://www.domain.com');
define('WP_HOME', 'https://www.domain.com');
}
//----------------------------------------
// Or you can use the current hostname automatically - but note that WordPress
// won't be able to redirect to the canonical domain for you
define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);
//----------------------------------------
// Or use .env files like Laravel does (you can also use it for database credentials)
// (Also run "composer require vlucas/phpdotenv")
require_once __DIR__ . '/../vendor/autoload.php';
$dotenv = new Dotenv\Dotenv(__DIR__ . '/..');
$dotenv->load();
define('WP_SITEURL', $_ENV['URL']);
define('WP_HOME', $_ENV['URL']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment