Skip to content

Instantly share code, notes, and snippets.

@cesc1989
Created October 11, 2017 17:30
Show Gist options
  • Save cesc1989/016e1102ceea5cca9195a9c0e82acfa8 to your computer and use it in GitHub Desktop.
Save cesc1989/016e1102ceea5cca9195a9c0e82acfa8 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