The ms_load_current_site_and_network() WordPress PHP function identifies the network and site of a requested domain and path and populates the corresponding network and site global objects as part of the multisite bootstrap process.
Usage
ms_load_current_site_and_network($domain, $path, $subdomain = false);
Parameters
$domain
(string) – The requested domain.$path
(string) – The requested path.$subdomain
(bool) – Whether a subdomain (true) or subdirectory (false) configuration. Default is false.
More information
See WordPress Developer Resources: ms_load_current_site_and_network()
Important: This function should not be used outside of core. It was introduced in WordPress 4.6.0 and wrapped into a function to facilitate unit tests.
Examples
Basic usage
This example demonstrates how to use ms_load_current_site_and_network() function for a subdomain configuration.
// Assuming a multisite with subdomain configuration $domain = 'subsite.example.com'; $path = '/'; $subdomain = true; ms_load_current_site_and_network($domain, $path, $subdomain);
Basic usage for subdirectory configuration
This example demonstrates how to use ms_load_current_site_and_network() function for a subdirectory configuration.
// Assuming a multisite with subdirectory configuration $domain = 'example.com'; $path = '/subsite/'; $subdomain = false; ms_load_current_site_and_network($domain, $path, $subdomain);
Custom redirect on site not found
This example shows how to use ms_load_current_site_and_network() function to redirect users to a custom URL if the site is not found.
$domain = 'unknown.example.com'; $path = '/'; $subdomain = true; $result = ms_load_current_site_and_network($domain, $path, $subdomain); if ($result === false) { wp_redirect('https://example.com/site-not-found'); exit; }
Custom error message on site not found
This example demonstrates how to display a custom error message if the site is not found using the ms_load_current_site_and_network() function.
$domain = 'unknown.example.com'; $path = '/'; $subdomain = true; $result = ms_load_current_site_and_network($domain, $path, $subdomain); if ($result === false) { echo "The requested site was not found."; exit; }
Using a filter to modify the domain
This example demonstrates how to use a filter to modify the domain before calling the ms_load_current_site_and_network() function.
$domain = 'subsite.example.com'; $path = '/'; $subdomain = true; // Modify the domain using a custom function $modified_domain = apply_filters('custom_domain_filter', $domain); ms_load_current_site_and_network($modified_domain, $path, $subdomain);