Yogesh Chauhan's Blog

WordPress: How to set WP_SITEURL?

in WordPress on December 28, 2021

What’s WP_SITEURL?

WP_SITEURL is used to define WordPress address (URL).

The address you define in WP_SITEURL, that’s where your WordPress core files live.

Basically it’s your WordPress root folder on your web host. So when you define WP_SITEURL, you need to include the http:// part too.

When you copy paste an URL, a trailing slash(/) will be included so if you copy and paste URL in the wp-config.php file, make sure to remove the trailing slash(/) at the end.

WP_SITEURL vs wp_options table value for siteurl

If you don’t have any value set in wp-config.php file, WordPress checks for it in the wp_options table.

If you set a WP_SITEURL in wp-config.php file, it’ll override the wp_options table value for siteurl.

Defining WP_SITEURL helps improve WordPress performance since your site needs to make a fewer database call and can easily find the siteurl from the wp-config.php file.

In case you want to change the siteurl from database using WP_SITEURL, you need to use RELOCATE constant.

How to define WP_SITEURL?

Use this line of code to define WP_SITEURL:


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

How to define WP_SITEURL dynamically?

You can dynamically set WP_SITEURL based on $_SERVER[‘HTTP_HOST’].


define( 'WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/path/to/wp-directory' );

You can dynamically set WP_SITEURL based on $_SERVER[‘SERVER_NAME’] too.


define( 'WP_SITEURL', 'http://' . $_SERVER['SERVER_NAME'] . '/path/to/wp-directory' );

HTTP_HOST vs SERVER_NAME

PHP used HTTP HOST Header value in the request to create HTTP_HOST dynamically. That might cause a problem by possibly creating a room for file inclusion vulnerabilities.

SERVER_NAME can be created dynamically too but if you configure Apache server and set UseCanonicalName “on”, then SERVER_NAME won’t be created dynamically anymore and SERVER_NAME value will be set by the server configuration.

It is safer to use SERVER_NAME over HTTP_HOST when UseCanonicalName is set “on” in Apache configurations and SERVER_NAME is not allowed to be created dynamically.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
An Introduction to wp-config file in WordPressWordPressSolution to pod install fails with json error on Mac OS X 10.15 (or Mac OS Catalina)MiscellaneousThe Types of DatabasesMiscellaneousHow to convert datetime to date format in JavaScript?JavaScriptSolution to “Call to undefined function mysql_error()” in RevSlider WordPress PluginWordPressHow to get ACF values from custom post type?WordPress