What distinguishes wp_redirect()
from wp_safe_redirect()
in WordPress?
The primary difference lies in their scope of redirection. wp_redirect()
is capable of redirecting to any URL, whereas wp_safe_redirect()
is more restrictive, permitting redirections only to the domain set as the Site Address in the General Settings. So, it only performs a local redirect by default.
How to extend the list of allowed domains for the wp_safe_redirect()?
You can extend the allowed domains by using allowed_redirect_hosts
filter.
For example:
add_filter( 'allowed_redirect_hosts', 'wph_extend_allowed_domains' );
function wph_extend_allowed_domains( $hosts ){
$hosts[] = 'example.com'; // Add your desired domain here
$hosts[] = 'wphandle.com';
return $hosts;
}
PS: It’s important to remember that wp_safe_redirect();
does not terminate script execution and should almost always be followed by an exit;
statement to ensure the redirection is properly executed and no subsequent code is run.
Looking for the best redirection strategy? Let WP Handle guide you. Check out our plans and streamline your website management today!