Change WordPress Home and Site URL using MySQL

By whgandalf | June 21, 2018

When you are frequently working with WordPress installations, it is common to be migrating servers from and to different providers or registars. Some customers also like to change the URL or maybe add https support to the current backup.

To explain this a little better I want to describe a migration from an old URL to a new URL as shown:

Old url: http://myoldurl.com/

New url: https://newweb.com/

You usually start restoring the data backup in a database and set the old WordPress files into the original structure from the last installation, but when you try to open the website (in this case https://newweb.com/) it doesn’t work.


At this moment you remember you have to log into the WordPress admin panel and change a couple of lines in general options:

WordPress General Options



Yeah, I know, you cannot access to that screen either, even if your WordPress admin URL is different it will be impossible for you to change it using this method, so here is where you have to try two different ones we propose.

Changing WordPress Home URL and Site URL via MySQL database

First, using your server command line get into MySQL CLI using some credentials with proper privileges, I will use the root user here typing the password when it request:

    ~: mysql -u root -p
    Enter password:

Select your production database, the one you restore to make the website work, in this case, that database is oldsitedb, to do it use the command USE like this:

   mysql> USE oldsitedb;

The selected database has the structure provided by WordPress developers in which you will find a table named wp_options, this table contains the key-value pairs you need to change, the key names are the home and siteurl so you can check what is the current value running SELECT * FROM wp_options WHERE option_name = 'home' and you will get:

   mysql> SELECT * FROM wp_options WHERE option_name = 'home';
    +----+------+------------+----+
    | option_id | option_name | option_value | autoload |
    +----+------+------------+----+
    | 39 | home | http://myoldurl.com/ | yes |
    +----+------+------------+----+
    1 row in set (0.00 sec)

same for the other key SELECT * FROM wp_options WHERE option_name = 'siteurl'.



Now that we know where is what we have to change, let’s do it using the next commands:

   mysql> UPDATE wp_options SET option_value="https://newweb.com/" WHERE option_name = "home";
   Query OK, 1 row affected (0.02 sec)
   mysql> UPDATE wp_options SET option_value="https://newweb.com/" WHERE option_name = "siteurl";
   Query OK, 1 row affected (0.02 sec)

you can always double check the change was applied as we did before:

   mysql> SELECT * FROM wp_options WHERE option_name = 'home';
    +----+------+------------+----+
    | option_id | option_name | option_value | autoload |
    +----+------+------------+----+
    | 39 | home | https://newweb.com/ | yes |
    +----+------+------------+----+
    1 row in set (0.00 sec)


Changing The Home and Site URL using WordPress PHP functions

If you do not like to be dealing with MySQL you can also change this values with other options using some WordPress features mixed with PHP.

Editing wp-config.php

Every WordPress installation has a wp-config.php file in the root folder, add the next lines with the new value for the variables like this:

    define('WP_HOME','https://newweb.com/');
    define('WP_SITEURL','https://newweb.com/');

This one is not the best solution but can be a temporary fix the access to the website; if you erase the lines, it will be broken again, those will be hardcoded values not stored in the database.

Editing functions.php

Every WordPress website is always rendered under a theme, and it can be either the default Twenty Seventeen or maybe another one bought by de last developer changing things around. You will find the themes location at wp-content/themes/ and after locating the folder containing the theme used by the website, you can change the file functions.php.

Add the next two lines to the beginning of the file, after the initial «<?php» line.

    update_option( 'siteurl', 'https://newweb.com/' );
    update_option( 'home', 'https://newweb.com/' );

Save the file and try to reload the website multiple times until you can see the changes applied.

comments powered by Disqus