Extend the List of Allowed Domains for wp_safe_redirect()

What is the difference between using wp_redirect() and wp_safe_redirect() functions actually?

The thing is that wp_redirect() allows to perform redirections just anywhere, but wp_safe_redirect() by default allows only the domain specified as Site Address (URL) in Settings > General.

In this tutorial I will show you how you can add any domain as an allowed domain for wp_safe_redirect() function using allowed_redirect_hosts action hook.

Here is how to do that:

add_filter( 'allowed_redirect_hosts', 'rudr_extend_allowed_domains_list' );

function rudr_extend_allowed_domains_list( $hosts ){
	
	$hosts[] = 'wordpress.com';
	$hosts[] = 'misha.rudrastyh.com';
	
	return $hosts;
	
}
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X