Override WordPress Site URL With wp-config.php

Wordpress is a CMS, which means that most of the data is and should be stored on the database. Two of the most important data that is stored in the database is the Home URL and the site URL.

Options Database Table

Along with many other options these two variables are stored in the wp_options database table.

The wp_options table stores all sorts of things you can use to aid the data shown on any of your WordPress themes. Some of the most common options are:

  • Site Name
  • Site URL
  • Site description
  • Admin Email
  • Permlink structure
  • Home page URL

All of these settings can be changed from the Settings section in your WordPress admin area. The wp_options database table is also used to store the values from the settings API.

If you are developing themes or plugins you can get at these easily by using the bloginfo() function.

Restore Dev Database

When you need to take the data from your live environment and transfer it to your dev environment you will also restore the variables in the options database table. This will mean that all your links in the dev environment will now point to your live URL.

When these are set to your Live URL you will not even be able to login to your admin area to change these options in the Settings section.

You have to login to your database and change these settings on the wp_options table, now you will be able to login to WordPress and use your Dev environment.

WordPress Site URL

The WordPress site URL WP_SITEURL is the value which defines where the core WordPress files will be. This values needs to include the http:// but should not include a trailing slash as this is added by WordPress when constructing links.

To set this in the Admin area go to Settings -> General settings.

This value is not always going to be the same as the Home URL as you could have WordPress in a subfolder, in this situation the site URL should be set to the where the WordPress files are.

define('WP_SITEURL', 'http://example.com/wordpress');

To get the site URL you can also use the inbuilt WordPress function site_url() which will return the value in the options database or the value from the wp-config.php file.

<?php echo site_url(); ?>

Home URL

The Home URL is the URL you want your visitors to type in to get to your Home page. Again with the WP_SITEURL, WP_HOME will override the value of the home URL in the database.

The value should include the http:// but not include the trailing slash.

define('WP_HOME', 'http://example.com');

To get the home URL you can also use the inbuilt WordPress function home_url() which will return the home URL value in the options database or the value in the wp-config.php.

<?php echo home_url(); ?>

Changing Options In wp-config.php

The best thing to do when switching between multiple environments is to setup your Site URL and Home URL on the wp-config.php. These two settings are not included in the original wp-config.php but can be included by using the define function.

Adding these in the wp-config.php will also improve performance as WordPress will not need to go to the database to get these values.

These two settings can be included in the wp-config since version 2.2 and act as an override to the database value. If you use the function bloginfo() to get the site URL.

<?php bloginfo('url'); ?>

Wordpress will go to the database to get this value, but if set in the wp-config.php WordPress will get the value from here instead.

To add these settings in the wp-config.php use the following code.

define('WP_HOME', 'http://www.example.com'); // no trailing slash
define('WP_SITEURL', 'http://www.example.com');  // no trailing slash

As these values override the values in the database once they are set in the wp-config.php they are removed from the admin area.

Advertise here 50% Off

Comment