Change url of a website in WordPress by Code

Have you changed the url of your website from settings —> general and messed it up?

change wp url

Sometimes we want to change the url of our website, we get confused and leave our website inaccessible.

I don't know if it has ever happened to you, it has happened to me 🙂

If it has happened to you, I will tell you step by step how to solve it and fix your website.

In this tutorial we are going to see 2 ways to change the url of our website by code:

For both methods you will need access to the ftp or cPanel of your server to be able to change the files of your website.

Change the url of your website from the WP-config.php file

As simple as adding these two lines in the wp-config.php file


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

By adding them you will see that the website no longer pays attention to what you had set in Settings —> General of your website.

Moreover, you will see that the option to modify them is now disabled.

If you remove the previous lines from the wp-config.php file they will be re-enabled.

Change the url of your website from the functions.php file of your template

This method is very similar to the previous one but with a big difference in substance.

We have to add these two lines in the functions.php file of our template.


update_option( 'siteurl', 'http://tuweb.com' );
update_option( 'home', 'http://tuweb.com' );

By doing so, we will see that, as with the previous method, the domain or URL of our website is changed.

Apparently it is the same but what happens underneath is very different.

While in the first method the value of the domain in the database continues to be what we put in Settings —> General, with this second method the value that we put there is changed. It is changed directly in the database.

We can say that the first method skip value from database, while in this second method what happens is that replaces the value from the database

url database

How the value is replaced in this second method, once the web is fixed, we must remove those lines from the functions.php file

Here you have the tutorial to change the domain by code in video:

Subscribe to my channel:  

I hope you liked it 🙂

Leave a comment