pre_get_network_by_path is a WordPress PHP filter that allows you to short-circuit the default logic used to determine a network by its domain and path. This can be helpful for optimizing your setup or customizing the network retrieval process.
Usage
add_filter('pre_get_network_by_path', 'your_custom_function', 10, 5); function your_custom_function($network, $domain, $path, $segments, $paths) { // your custom code here return $network; }
Parameters
$network
null|false|WP_Network: Network value to return by path. Default null to continue retrieving the network.$domain
string: The requested domain.$path
string: The requested path, in full.$segments
int|null: The suggested number of paths to consult. Default null, meaning the entire path was to be consulted.$paths
string[]: Array of paths to search for, based on$path
and$segments
.
More information
See WordPress Developer Resources: pre_get_network_by_path
Examples
Bypassing network retrieval for a specific domain
If you want to bypass network retrieval for a specific domain, you can use the following code:
add_filter('pre_get_network_by_path', 'bypass_network_retrieval', 10, 5); function bypass_network_retrieval($network, $domain, $path, $segments, $paths) { if ($domain === 'example.com') { return false; } return $network; }
Custom logic for network retrieval
Implement custom logic for network retrieval by replacing the default logic with your own:
add_filter('pre_get_network_by_path', 'custom_network_retrieval', 10, 5); function custom_network_retrieval($network, $domain, $path, $segments, $paths) { // Implement your custom logic here if (/* your custom condition */) { return wp_get_network(/* your custom arguments */); } return $network; }
Retrieve network from cache
Retrieve the network from cache to optimize performance:
add_filter('pre_get_network_by_path', 'retrieve_network_from_cache', 10, 5); function retrieve_network_from_cache($network, $domain, $path, $segments, $paths) { $cached_network = get_network_from_cache($domain, $path); if ($cached_network) { return $cached_network; } return $network; }
Logging network retrieval requests
Log network retrieval requests for debugging or analysis purposes:
add_filter('pre_get_network_by_path', 'log_network_retrieval', 10, 5); function log_network_retrieval($network, $domain, $path, $segments, $paths) { error_log("Network retrieval request: domain: $domain, path: $path"); return $network; }
Modify the number of path segments
Modify the number of path segments to consult based on specific conditions:
add_filter('pre_get_network_by_path', 'modify_path_segments', 10, 5); function modify_path_segments($network, $domain, $path, $segments, $paths) { if (/* your custom condition */) { $segments = 2; // Change the number of path segments to consult } return $network; }