I wanted to change WordPress URL, you won’t believe what happened afterwards

WordPress has a nasty habit of storing absolute URLs in the database.  That means that all internal paths, including images and other resources are stored in the database containing the base URL of your blog alongside the URI of the resource. It is not possible to change WordPress URL quickly. Or at least I thought so.

For years, I’ve been changing WordPress URL in the same way – export the database, search/replace old URL with new one in the SQL file, then import changed SQL file. And it worked. It was not quick, but it worked. Then, one day, I needed to change WordPress URL, but I didn’t have access to database. So, I decided to look for another solution. And, voilà, I found out that it can be done just by adding two lines of code!
There are actually several ways to do this. But easiest by far is to add these two lines to the beginning of the active theme’s functions.php:

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

Then, all you have to do is to load the login or admin page a couple of times. The site should come back up. And, it’s done! Amazing!

All of this time I was doing it the hard way. And I was teaching people to do it like that. Anyway, this is a good lesson that from time to time, person should question the way he does things and try to find out a better, more efficient solution. Who knows what we could accomplish if we did that more often.

For further reading, I recommend official WordPress codex page with all the different ways to change the URL or migrate the site explained in details: https://codex.wordpress.org/Changing_The_Site_URL

By Aleksandar Jakovljević

My name is Aleksandar Jakovljević and I am a web developer from Belgrade, Serbia. I was born in Smederevo, in 1980. I started focusing on web development since late 2003. I am mostly focused on open source web technologies. I specialised in PHP, focusing on Symfony framework, with skills in Drupal and WordPress, too. I worked more than 7 years for art_net studio. I work for Computer Rock (former Spoiled Milk) since April 2011. I love the web and everything related to it. The possibility to publish your ideas quickly and make them available to the entire world is what made me decide to pursue a career in web development.

Leave a comment

Your email address will not be published. Required fields are marked *