Create Wordpress Theme From Scratch! Build Your Own

WordPress function for site url to enable homepage link

webapps log

webapps Lab
September 26, 2020

wordpress function for site url

You have designed a WordPress page with a header containing the site name without the site URL.

But the blog name without a link is hard to navigate back to the home page, you need to call a WordPress function for the site URL to solve this.

wordpress function for site url

The function will make the title name of the blog clickable and easy to go back to the blog homepage.


The question is, which WordPress function does retrieve the home URL of the website?

site_url and home_url are WordPress functions, which fetch the home URL of the website or a blog.

The two functions return the same output, which is the home URL but they differ. How? you will know after seeing how to use site_url and home_url for the homepage link.

The other WordPress functions that will bring website site url are get_site_url and get_home_url

Are you confused with various  WordPress functions that return similar values?

Worry out and dive in to know the usage, difference, and similarity of each function (site_url,home_url,get_site_url,and get_home_url).

home_url usage for site URL

You will get the site URL by echoing home_url in PHP codes.

<?php echo home_url();?>

The above PHP code is to be written as a value of the href attribute of the anchor tag.

This code line below shows how to use <?php echo home_url();?> in the anchor tag.

<h1><a href="<?php echo home_url();?>"><?php bloginfo('name');?> | WebAppLabs255</a></h1>

For example, check this my HTML source before I implemented the home_url function

site url HTML code.

1.<!DOCTYPE html>
3.   <head>
4.      <title>Responsive Design | WebAppLabs255</title>
5.    <link href="<?php bloginfo('stylesheet_url');?>" rel="stylesheet">
6.        <meta name="viewport" content="width=device-width,initial-scale=1.0">
7.    </head>
8.    <body>
9.        <div class="header">
10.            <h1><a href="#"><?php bloginfo('name');?> | WebAppLabs255</a></h1>
11.        </div>
12.        <div class="row">
13.            <nav class="nav-1">
14.            <?php
15.            $args=array('theme_location'=>'primary');
16.            ?>
17.            <?php wp_nav_menu($args);?>
18.            </nav>
19.        </div>

Line 10 sets the home page title. The title is not clickable, so, Edit the line by changing href="#" to href="<?php echo home_url();?>".

My site URL is http://localhost/withoutbootstrap, after editing the line home_url return my site URL as it is seen on this image.

site_url_home_url functions

how home_url retrieve site URL?

home_url retrieves site URL by returning get_home_url function.

get_home_url returns the string that is a link to the website.

The raw source code is available in the include directory within the link-template.php file

To understand how home_url returns the site URL, you should check the home_url raw function.

You will find the function in link-template.php starting at line number 3170.

3170. function home_url( $path = '', $scheme = null ) {
3171.     return get_home_url( null, $path, $scheme );
3172.   }

home_url passes in two parameters, which are the option to pass in the parameter. Each option parameter has a special task.

$path – this enables you to pass a path relative to the home URL, you can let it empty if you don’t want to customize the home URL.

 For example, if the current domain of your website is youdomain.com and you have another index page in another directory let’s call the directory myanotherdirectory.

So, if you want the index.php page inside myanotherdirectory to be a site URL, the home_url usage will change as follows.

<?php home_url('myanotherdirectory');?>

If your site URL was youdomain.com, the site URL will change to youdomain.com/myanotherdirectory.

$scheme –  It is a variable for customizing site URL protocol. The variables accept only four protocols as schemes. The acceptable schemes are http, https, rest, relative, and the default value that is null.

If home_url() returns http protocol instead of https, pass the argument of the https protocol as home_url(‘https’).

The URL output will be https://yourdomain.com

You can pass in all two variables in the home_url function

<?php home_url('myanotherdirectory','https');?>

But it is not necessary to pass the argument because this function is able to output the home URL with a required scheme.

get_home_url and home_url

get_home_url is another WordPress function that gives you a home URL of your WordPress site. get_home_url and home_url work together.

Without get_home_url, the home_url cannot work because it uses get_home_url  to retrieve the URL of the website.

The variables parameters of $path and $scheme are going directly to the get_home_url.

You can use get_home_url for site URL in HTML codes as follows.

Go back site URL HTML code above that I posted at line number 10.

<h1><a href="#"><?php bloginfo('name');?> | WebAppLabs255</a></h1>

Replace href="#" with href ="<?php echo get_home_url();?>"

get_home_url can receive $path and $scheme as like as home_url.

site_url usage for site URL

site_url provides the site URL with respect to where WordPress framework files and folders reside.

Examples of those WordPress application files and folders are wp-admin, wp-content, wp-blog-header.php, and others.


site_url wp-blog-header

For example, if you have a domain of http://localhost but WordPress application files are not in the root directory.

Instead, the files are in another sub-directory, I named it withoutbootstrap, calling the site_url function will output http://localhost/withoutbootstrap as home or site URL.

To use the site_url function to output your website home URL link, you will apply the following PHP code in the anchor tag.

<?php echo site_url();?>

In HTML anchor tag code …

<h1><a href="<?php echo site_url();?>"><?php bloginfo('name');?> | WebAppLabs255</a></h1>

how site_url function retrieve site URL?

site_url returns get_site_url to fetch the homepage link of the site. As home_url, site_url also receives two similar parameters.

These parameters are $path and $scheme.

So, if you want to customize the URL path, pass in string in site_url, for customizing a protocol pass your desired protocol like https or http.

site_url and get_site_url

site_url is just one of the wordpress functions for site url that retrieves url string by returning get_site_url function.

If you open the include folder, inside the link-template.php file at the line starting number 3246 to 3248, you will find site_url raw code returning the get_site_url function.

3246. function site_url( $path = '', $scheme = null ) {
3247. return get_site_url( null, $path, $scheme );
3248. }

The variables passed in site_url go to get_site_url for processing.

Although get_site_url receives three parameters. Since site_url passes only two variables, the first variable of get_site_url becomes a null

return get_site_url( null, $path, $scheme );

The three parameters of get_site_url function.

  1. blog_id – it an optional value that lets you set the id of the current site if you have more than one site. the default value is null.
  2. $path for customizing site url, the default is empty.
  3. $scheme for changing url protocol.

how to use the get_site_url function for wordpress site url?

You retrieve the home URL of the blog or any WordPress website by echoing get_site_url() in a PHP code format.

<?php echo get_site_url();?>

And you can implement this in the anchor tag element of html code.

<h1><a href="<?php echo get_site_url();?>"><?php bloginfo('name');?> | WebAppLabs255</a></h1>

So, if my website url is mydomain.com

The WordPress code output a link to the website http://mydomain.com.

And incase the WordPress installation files were stored in a subfolder the site url will be http://mydomain.com/subfolder.

But if you want to change the path of the url and scheme, just set $path and $scheme parameters.

<?php echo get_site_url(null, 'another_directory','https');?>

The new output for site url will be https://mydomain.com/another_directory

how get_site_url work?

get_site_url passes in three parameters which are $blog_id, $path, and $scheme.

To understand how the function uses these parameters to retrieve the current blog url, you should check the function’s raw php codes.

1. function get_site_url( $blog_id = null, $path = '', $scheme = null ) {
2. if ( empty( $blog_id ) || ! is_multisite() ) {
3. $url = get_option( 'siteurl' );
4. } else {
5. switch_to_blog( $blog_id );
6. $url = get_option( 'siteurl' );
7. restore_current_blog();
8. }

9. $url = set_url_scheme( $url, $scheme );

10. if ( $path && is_string( $path ) ) {
11. $url .= '/' . ltrim( $path, '/' );
12. }

13. return apply_filters( 'site_url', $url, $path, $scheme, $blog_id );
14. }

Line 2 checks if the blog doesn’t have any id and it is not running on a multisite network.

Remember, wordpress has a feature that supports multiple websites to use the same wordpresss installation.

If the blog doesn’t use id and it is not on a multisite network, the get_option(‘siteurl’) function gives url string of the current website.

The process takes place at line number 6.

But if the integer blog id has been provided, switch_to_blog( $blog_id ) retrieves all the information on the blog of the given id.

switch_to_blog function pulls the data of another blog, so the current blog information must be restored.

Therefore, restore_current_blog() restores the data of the very current blog.

get_site_url also passes $scheme that is the protocols.

Line number 9 instance variable $url is set to set_url_scheme($url,$cheme).

set_url_scheme returns the string of the current site url with http or https protocols like https://mydomain.com.

Line 10 checks if the path(ie directory name) value has been provided.

If it is true, the $url variable is appended with a slash (‘/’) and then append ed by the provided path.

The statement will make the url look like this format http://mydomain.com/directory_path.

Line 14 uses apply_filter function to return site url through passing five parameters (site_url, $url, $path, $scheme,$blog_id).

apply_filter returns the site URL

That is how get_site_url works to output home page url.

why home_url over site_url

home_url returns the site url intended to be used by a front end user.

You may have many domains but you don’t want the address that hosts wordpress framework file to be accessed by a user.

For that purpose, wordpress allows you to set another address that will prevent the installation directory address to be used as a site home page.

For example, the wordpress installation address(domain) is http://wordpressdomain.com, you can set a new home address to http://mydomain.com.

Therefore, if you call home_url instead of site_url the home_url will return http://mydomain.com instead of http://wordpressdomain.com.

To set the home page that doesn’t use wordpress installation domain.

  1. Open wordpress admin dashboard
  2. Head over to the setting->general for the general settings page
  3. At the site address(URL) field, Enter your new address for the site home page


home_url function uses the address entered in Site Address(URL).

It is very advantageous to use two distinct addresses for the security purpose that is why I recommend home_url over site_url for site URL as the home page of the blog.

when to use get_home_url and get_site_url for the site home page?

If you run many blogs that are on the multisite network, uses get_home_url or get_site_url and not site_url and home_url.

These two functions allow you to retrieve the data of one of the blogs you run.

There is wordpress function for fetching blog id, you don’t have to remember the id numbers of all blogs.

But the get_current_blog_id() function will give you the id of a current blog that you need its data.

How to use get_home_url and get_site_url?

It is simple, just write <?php get_home_url(get_current_blog_id());?> or <?php get_site_url(get_current_blog_id());?>


Use site_url or home_url or get_site_url or get_home_url for retrieving site url. If you want to separate the installation address from the address that the user type in the browser, use home_url or get_home_url.

And avoid site_url and home_url to get the home page link if a site will be on a multisite network. get_home_url and get_site_url are the best functions for multisite site url.

You can use even use home_url or site_url in wp_nav_menu to add li a list item in the custom menu.