Using the network_home_url function in WordPress

The network_home_url function in WordPress is designed to retrieve the home URL for the current network. This function is part of WordPress’ Multisite feature, which allows you to manage multiple WordPress sites from a single WordPress installation.

The function returns the home URL of the current site being viewed in a network, regardless of the site within the network that the function is called from. This can be particularly relevant when developing plugins or themes that need to link back to the network’s main site or when you need to redirect users to the network’s home page.

It’s important to note that the network_home_url function will return the same URL regardless of whether it’s called from the main site or a subsite within the network. This is because the function is designed to always return the home URL of the network, not the home URL of the individual site.

While the network_home_url function is specifically designed for use with WordPress’ Multisite feature, it can also be used in a standard WordPress installation. In this case, the function will simply return the home URL of the single WordPress site.

Parameters Accepted by the network_home_url Function

The network_home_url function in WordPress accepts two parameters:

  • $path (string): This is an optional parameter. By default, its value is an empty string (”). It is used to specify a path that is relative to the home URL.
  • $scheme (string|null): This is also an optional parameter with a default value of null. It is used to provide a context to the home URL. It can accept either ‘http’, ‘https’, or ‘relative’.

Return Value of the network_home_url Function

The network_home_url function returns a string. This string is the home URL link, which may have an optional path appended to it, depending on the parameters provided to the function.

If the function doesn’t accept any parameters, it simply returns the home URL link without any path appended.

Examples

Example 1: Basic Usage

<a href="<?php echo esc_url(network_home_url()); ?>">Network Home</a>

This example creates a simple link to the network’s home page. The network_home_url() function retrieves the URL of the main site in a multisite network, and esc_url() is used to ensure the URL is safe to output in an HTML attribute.

Example 2: Adding a Path to the URL

<a href="<?php echo esc_url(network_home_url('/about-us/')); ?>">About Us</a>

Here, network_home_url() is used with an additional path argument to create a link directly to the ‘About Us’ page of the network’s main site. The function automatically handles concatenating the path to the main site’s home URL.

Conclusion

The network_home_url function in WordPress is an useful feature that retrieves the home URL for the current network. It is particularly beneficial when working within a multisite network, as it provides the ability to generate the correct home URL regardless of the site currently being viewed. This function is commonly used in plugins and themes to ensure that links and references are always directed to the correct location, enhancing the consistency and reliability of the network’s operation.

Related WordPress Functions