The network_by_path_segments_count WordPress PHP filter allows you to modify the number of path segments considered when searching for a site within a multisite network.
Usage
add_filter('network_by_path_segments_count', 'my_custom_path_segments_count', 10, 3); function my_custom_path_segments_count($segments, $domain, $path) { // your custom code here return $segments; }
Parameters
- $segments (int|null): The number of path segments to consider. WordPress by default looks at one path segment. The function default of null only makes sense when you know the requested path should match a network.
- $domain (string): The requested domain.
- $path (string): The requested path, in full.
More information
See WordPress Developer Resources: network_by_path_segments_count
Examples
Change path segments count to two
Modify the number of path segments considered to two.
add_filter('network_by_path_segments_count', 'my_custom_path_segments_count', 10, 3); function my_custom_path_segments_count($segments, $domain, $path) { $segments = 2; return $segments; }
Change path segments count based on domain
Modify the number of path segments considered depending on the domain.
add_filter('network_by_path_segments_count', 'my_custom_path_segments_count', 10, 3); function my_custom_path_segments_count($segments, $domain, $path) { if ($domain === 'example.com') { $segments = 3; } return $segments; }
Change path segments count based on path
Modify the number of path segments considered depending on the path.
add_filter('network_by_path_segments_count', 'my_custom_path_segments_count', 10, 3); function my_custom_path_segments_count($segments, $domain, $path) { if (strpos($path, 'custom_path') !== false) { $segments = 4; } return $segments; }
Reset path segments count to default
Reset the number of path segments considered to the default value.
add_filter('network_by_path_segments_count', 'my_custom_path_segments_count', 10, 3); function my_custom_path_segments_count($segments, $domain, $path) { $segments = null; return $segments; }
Set path segments count based on a custom logic
Set the number of path segments considered based on custom logic.
add_filter('network_by_path_segments_count', 'my_custom_path_segments_count', 10, 3); function my_custom_path_segments_count($segments, $domain, $path) { // Check if the path contains a specific string if (strpos($path, 'special') !== false) { $segments = 5; } else { $segments = 3; } return $segments; }