Using WordPress ‘network_site_url’ PHP filter

The network_site_url filter allows you to modify the network site URL in a WordPress multisite setup.

Usage

add_filter('network_site_url', 'your_custom_function', 10, 3);

function your_custom_function($url, $path, $scheme) {
  // your custom code here
  return $url;
}

Parameters

  • $url (string) – The complete network site URL, including the scheme and path.
  • $path (string) – Path relative to the network site URL. Empty string if no path is specified.
  • $scheme (string|null) – Scheme to give the URL context. Accepts ‘http’, ‘https’, ‘relative’, or null.

Examples

Force HTTPS for network site URL

add_filter('network_site_url', 'force_https_network_site_url', 10, 3);

function force_https_network_site_url($url, $path, $scheme) {
  return set_url_scheme($url, 'https');
}

This code forces the network site URL to always use the HTTPS scheme.

Add a custom path to the network site URL

add_filter('network_site_url', 'add_custom_path_to_network_site_url', 10, 3);

function add_custom_path_to_network_site_url($url, $path, $scheme) {
  return $url . '/custom-path';
}

This code appends a custom path /custom-path to the network site URL.

Modify the network site URL for a specific site

add_filter('network_site_url', 'modify_specific_site_network_site_url', 10, 3);

function modify_specific_site_network_site_url($url, $path, $scheme) {
  if (get_current_blog_id() == 2) {
    return 'https://example.com/custom-url';
  }
  return $url;
}

This code changes the network site URL to ‘https://example.com/custom-url‘ for the site with blog ID 2.

Remove the “www” subdomain from the network site URL

add_filter('network_site_url', 'remove_www_subdomain_from_network_site_url', 10, 3);

function remove_www_subdomain_from_network_site_url($url, $path, $scheme) {
  return str_replace('://www.', '://', $url);
}

This code removes the “www” subdomain from the network site URL.

Change the URL scheme to relative

add_filter('network_site_url', 'set_relative_scheme_network_site_url', 10, 3);

function set_relative_scheme_network_site_url($url, $path, $scheme) {
  return set_url_scheme($url, 'relative');
}

This code sets the URL scheme to relative, making the network site URL protocol-relative.