This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
network_site_url filter
Filters the network site URL.
To use network_site_url filter, first you have to register it using add_filter. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.
We at WePlugins always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.
In the below live example, we have defined a function modify_network_site_url_defaults which takes 3 parameters and we registered using add_filter. The first parameter network_site_url is name of the hook, The second parameter modify_network_site_url_defaults is name of the function which need to be called, third parameter is the priority of calling the hook if same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.
Sometime, you have to remove a registered hook so you can use remove_filter to remove network_site_url filter.
Parameters
Below are the 3 parameters required to use this hook.
- $url : (string) The complete network site URL including scheme and path.
- $path : (string) Path relative to the network site URL. Blank string if no path is specified.
- $scheme : (string|null) Scheme to give the URL context. Accepts ‘http’, ‘https’, ‘relative’ or null.
Live Example
apply_filters( 'network_site_url', string $url, string $path, string|null $scheme )
Below is an example how you can use this hook.
Example 1: Basic Usage
This example demonstrates the basic usage of the network_site_url filter.
function weplugins_modify_network_site_url_defaults($url, $path, $scheme) { // Update the $url variable according to your website requirements and return this variable. return $url; } // Add the filter add_filter( "network_site_url", "weplugins_modify_network_site_url_defaults", 10, 3 );
Example 2: Conditional Modification
In this example, the URL is modified conditionally based on the $scheme parameter.
function weplugins_conditional_modify_network_site_url($url, $path, $scheme) { // Modify the URL conditionally if ($scheme === 'https') { $url = str_replace('http://', 'https://', $url); } return $url; } // Add the filter add_filter( "network_site_url", "weplugins_conditional_modify_network_site_url", 10, 3 );
Example 3: Removing the Hook
To remove a hook callback, use the example below.
function weplugins_modify_network_site_url_defaults($url, $path, $scheme) { return $url; } // Remove the filter remove_filter( "network_site_url", "weplugins_modify_network_site_url_defaults", 10, 3 );
Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
If you’re having any trouble using this hook, please contact our WordPress Development Team and we’d be happy to assist you.
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.