How to Extend the List of Allowed Domains for wp_safe_redirect()

By default function wp_safe_redirect() allows to redirect only to the same domain which is presented as a home_url(). But in some cases you may want to extend it with let’s say subdomains and in this tutorial I will provide a ready snippet for that.

It is one of my shortest tutorials ever. But I decided to publish it anyway because I didn’t find any information about it anywhere.

WordPress home_url() in general settings
WordPress home URL in Settings > General. By default wp_safe_redirect() allows to redirect only to this domain excluding subdomains.
add_filter( 'allowed_redirect_hosts', 'misha_extend_allowed_domains_list' );
 
function misha_extend_allowed_domains_list( $hosts ){
 
	$hosts[] = 'wordpress.com';
	$hosts[] = 'misha.rudrastyh.com';
 
	return $hosts;
 
});

That’s all 😁

Read also

Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WordPress?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Leave a comment

php js HTML CSS Code

I will only use your personal information to contact you. Privacy Policy