The allowed_redirect_hosts WordPress PHP filter allows you to modify the list of allowed hosts that your WordPress site can redirect to.
Usage
add_filter('allowed_redirect_hosts', 'my_custom_allowed_redirect_hosts', 10, 2);
function my_custom_allowed_redirect_hosts($hosts, $host) {
// your custom code here
return $hosts;
}
Parameters
$hosts(string[]): An array of allowed host names.$host(string): The host name of the redirect destination; empty string if not set.
More information
See WordPress Developer Resources: allowed_redirect_hosts
Examples
Allow a custom domain for redirection
add_filter('allowed_redirect_hosts', 'allow_my_custom_domain', 10, 2);
function allow_my_custom_domain($hosts, $host) {
$hosts[] = 'my-custom-domain.com';
return $hosts;
}
Allow subdomains for a specific domain
add_filter('allowed_redirect_hosts', 'allow_subdomains', 10, 2);
function allow_subdomains($hosts, $host) {
if (preg_match('/(.+)\.example\.com/', $host)) {
$hosts[] = $host;
}
return $hosts;
}
Block a specific domain from redirection
add_filter('allowed_redirect_hosts', 'block_specific_domain', 10, 2);
function block_specific_domain($hosts, $host) {
$blocked_domain = 'blocked-domain.com';
if (($key = array_search($blocked_domain, $hosts)) !== false) {
unset($hosts[$key]);
}
return $hosts;
}
Allow multiple custom domains for redirection
add_filter('allowed_redirect_hosts', 'allow_multiple_custom_domains', 10, 2);
function allow_multiple_custom_domains($hosts, $host) {
$additional_hosts = ['custom-domain-1.com', 'custom-domain-2.com'];
$hosts = array_merge($hosts, $additional_hosts);
return $hosts;
}
Allow all subdomains of the current domain
add_filter('allowed_redirect_hosts', 'allow_all_subdomains', 10, 2);
function allow_all_subdomains($hosts, $host) {
$current_domain = parse_url(get_site_url(), PHP_URL_HOST);
if (preg_match('/(.+)\.' . preg_quote($current_domain) . '/', $host)) {
$hosts[] = $host;
}
return $hosts;
}