The domain_exists WordPress PHP filter checks if a site name is already taken. The name can be a site’s subdomain or subdirectory path, depending on the network settings.
Usage
add_filter('domain_exists', 'your_custom_function', 10, 4);
function your_custom_function($result, $domain, $path, $network_id) {
// your custom code here
return $result;
}
Parameters
$result: (int|null) The site ID if the site name exists, null otherwise.$domain: (string) Domain to be checked.$path: (string) Path to be checked.$network_id: (int) Network ID. Only relevant on multi-network installations.
More information
See WordPress Developer Resources: domain_exists
Examples
Check for Custom Domain Conflicts
Prevent site creation if the domain contains “example.com”
add_filter('domain_exists', 'prevent_example_domain', 10, 4);
function prevent_example_domain($result, $domain, $path, $network_id) {
if (strpos($domain, 'example.com') !== false) {
return true;
}
return $result;
}
Limit Subdirectory Depth
Disallow site creation if the subdirectory depth is more than 2 levels
add_filter('domain_exists', 'limit_subdirectory_depth', 10, 4);
function limit_subdirectory_depth($result, $domain, $path, $network_id) {
if (substr_count($path, '/') > 2) {
return true;
}
return $result;
}
Block Certain Domain Names
Disallow site creation if the domain name matches a list of blocked domains
add_filter('domain_exists', 'block_domains', 10, 4);
function block_domains($result, $domain, $path, $network_id) {
$blocked_domains = array('blocked.com', 'forbidden.net');
if (in_array($domain, $blocked_domains)) {
return true;
}
return $result;
}
Allow Specific Domain Only
Allow site creation only if the domain name contains a specific keyword
add_filter('domain_exists', 'allow_specific_domain', 10, 4);
function allow_specific_domain($result, $domain, $path, $network_id) {
if (strpos($domain, 'allowed') === false) {
return true;
}
return $result;
}
Custom Validation Based on Network ID
Validate the domain and path based on the network ID
add_filter('domain_exists', 'custom_validation_based_on_network', 10, 4);
function custom_validation_based_on_network($result, $domain, $path, $network_id) {
if ($network_id == 1 && strpos($domain, 'network1') === false) {
return true;
}
if ($network_id == 2 && strpos($path, 'network2') === false) {
return true;
}
return $result;
}