Have you ever tried to change the WordPress Site URL using your MySQL command line, or using PHP code? This is what this guide is about. After going through the steps of this guide, you will be able to change your site URL to whatever value is necessary.
Table of contents
- What is the WordPress Site URL?
- How to change the WordPress Site URL using MySQL?
- How to change the WordPress Site URL using PHP?
- Conclusion
What is the WordPress Site URL?
The site URL of a website developed with WordPress is the internet location (or “address”) that you want people to type in their browser so that they can reach your WordPress website.
This URL controls where WordPress is located on the web. Note that WordPress does not manage your domain name and DNS settings, you would usually configure those separately using your domain name provider.
Importantly, a change of the site URL may invalidate your session cookie and will then log you out of your session, also for admin sessions.
How to change the WordPress Site URL using MySQL?
First, you will need your database connection credentials. If you haven’t backed-up these, or you don’t find them anymore, you can open the wp-config.php
file and look out for: DB_NAME
, DB_USER
and DB_PASSWORD
.
Take note of the values of these variables in your WordPress configuration and use them in the following command:
mysql -u DB_USER -p DB_NAME
Make sure to replace DB_USER
with the correct username and DB_NAME
with the correct database name. You will be prompted to enter the password when you hit enter.
How to read the WordPress Site URL using MySQL?
We’ll have a quick look at a MySQL query that lets you read the WordPress Site URL, that will permit to find out which is the current Site URL of your website.
SELECT option_name, option_value
FROM wp_options
WHERE option_name IN ('home', 'siteurl');
Pretty straight forward, isn’t it? With this MySQL query, we are selecting the option name and value for any WordPress option of which the name is either home
or siteurl
.
When you execute this query on the MySQL prompt, it will display two rows and if everything is well, the two options should have the same value, too.
How to update the WordPress Site URL using MySQL?
Next, we’ll write a query that changes the site URL to a new value, including the https (or http) scheme and that contains the full URL to your website.
This query is a simple MySQL UPDATE query that you can execute in your terminal using the MySQL prompt or using phpMyAdmin or any other DBMS tools to connect to MySQL.
UPDATE wp_options
SET option_value = 'https://example.com'
WHERE option_name IN ('home', 'siteurl');
And that’s it already, your WordPress Site URL has been changed and this change will be reflected as soon as you browse over to your website again.
How to change the WordPress Site URL using PHP?
And what happens if you don’t have access to MySQL? Well, you’ll have to change the Site URL using PHP. It’s even simpler, I promise.
There is two ways to change it using PHP. The first one is using the WP_HOME
and WP_SITEURL
constants that are defined with any WordPress installation. And the other one is using the home
and siteurl
options with the update_option()
function available with the WordPress API.
Set the WordPress Site URL using wp-config.php
Open the wp-config.php
source code file and place your cursor at the end of the file. Then you can insert the following two lines of code, replacing the URL with your desired URL of course:
define( 'WP_HOME', 'https://example.com' );
define( 'WP_SITEURL', 'https://example.com' );
Obviously, you should the example.com
for your desired site URL. If you put this inside the wp-config.php
file, it will be executed on every page load, and thereby will make sure that these constants are always set to the value you desired.
Set the WordPress Site URL using functions.php
Sometimes, when you access a website using the FTP protocol, it may be that you don’t have access to MySQL or that you don’t have access to the wp-config.php
file either.
In this case, you will have to edit the current active theme’s functions.php
. It is recommended that you do not modify primary themes, but always use child themes for such modifications.
Open the wp-content/themes/your-theme-name/functions.php
source code file and place your cursor at the end of the file. Then you can insert the following two lines of code, replacing the URL with your desired URL of course:
update_option( 'siteurl', 'https://example.com' );
update_option( 'home', 'https://example.com' );
The difference here, is that you are setting the WordPress options value, rather than using the PHP constants (that may be set before).
And that’s it as well! You are all set and know how to change the WordPress Site URL for sure now, don’t you? Make sure to add a comment if you find other ways to achieve the same!
Conclusion
Once you have done the above changes, you should be able to access your WordPress website again. The WordPress Site URL is used to determine a canonical URL for your website. Hopefully you are able to solve your issues.
If you are looking for another framework to develop your web application, I recommend that you have a read here, with details on how to build your own SaaS with Laravel and PHP.
[…] listed method can be freely used within WordPress installations as well, make sure to have a read here if you are looking for information about changing WordPress […]
[…] on a VPS Server Add Alpine.js to a Laravel project How to manage files and folders with PHP Change the WordPress Site URL How to Write a Makefile to Compile C or […]
[…] on a VPS Server Add Alpine.js to a Laravel project How to manage files and folders with PHP Change the WordPress Site URL How to Write a Makefile to Compile C or […]