Using WordPress ‘network_home_url’ PHP filter

The network_home_url filter allows you to modify the network home URL in a WordPress multisite setup.

Usage

add_filter('network_home_url', function($url, $path, $orig_scheme) {
    // Your custom code here
    return $url;
}, 10, 3);

Parameters

  • $url (string): The complete network home URL, including scheme and path.
  • $path (string): Path relative to the network home URL. It’s a blank string if no path is specified.
  • $orig_scheme (string|null): Scheme to give the URL context. Accepts ‘http’, ‘https’, ‘relative’, or null.

Examples

Force HTTPS

add_filter('network_home_url', function($url) {
    return set_url_scheme($url, 'https');
}, 10, 1);

This code forces the network home URL to always use the ‘https’ scheme.

Add a custom path

add_filter('network_home_url', function($url) {
    return $url . '/custom-path';
}, 10, 1);

This code adds ‘/custom-path’ to the network home URL.

Change the domain

add_filter('network_home_url', function($url, $path, $orig_scheme) {
    $parsed_url = parse_url($url);
    $new_domain = 'new-domain.com';
    return $orig_scheme . '://' . $new_domain . $path;
}, 10, 3);

This code changes the domain of the network home URL to ‘new-domain.com’.

Remove ‘www’ from the domain

add_filter('network_home_url', function($url, $path, $orig_scheme) {
    $parsed_url = parse_url($url);
    $domain = str_replace('www.', '', $parsed_url['host']);
    return $orig_scheme . '://' . $domain . $path;
}, 10, 3);

This code removes the ‘www’ subdomain from the network home URL.

Add a query string

add_filter('network_home_url', function($url) {
    return add_query_arg('key', 'value', $url);
}, 10, 1);

This code adds a query string with a key ‘key’ and a value ‘value’ to the network home URL.