Hook allow_redirect_hosts in WordPress

wordpress allowed redirect hosts
27 June 2025

Introduction to the Hook allow_redirect_hosts in WordPress

WordPress is a highly versatile and powerful content management system that allows us to add various features through hooks. One of these hooks is allow_redirect_hosts. This hook allows us to specify the list of permitted hosts for usage in redirect URLs. In fact, this capability helps enhance the security of WordPress sites.

Whenever we want to redirect a user to an external URL, we must ensure that this URL is valid and secure. By using the allow_redirect_hosts hook, we can define the hosts that can be approved for them. This task is especially essential when you are utilizing a backend to create an API or a webhook with external systems; security is quite critical.

Using this hook is simple, and within a function, we can easily add new allowed hosts. If you want to add a new allowed host, just adding a suitable function in the functions.php file of your theme is enough. This way, you can have more control over the URLs users are directed to.

Following is a simple code example for how to use the allow_redirect_hosts hook. By using this code, you can add specific hosts to the list of allowed hosts. So, let’s take a look at the code.

function my_custom_redirect_hosts( $allowed_hosts ) {
$allowed_hosts[] = 'example.com'; // Add example.com to the list of allowed hosts
return $allowed_hosts;
}
add_filter( 'allowed_redirect_hosts', 'my_custom_redirect_hosts' );

Code Explanation

Here, we have created a function named my_custom_redirect_hosts that accepts an array of allowed hosts. This function adds a new host to the array; in this case, example.com, which indicates that this host is allowed from a security perspective.


Within this function, we add a new element to the array $allowed_hosts that indicates our new host. Here example.com can be replaced with any valid host.


Then, we use the add_filter function to attach our function to the hook allowed_redirect_hosts. This action allows WordPress to use our function during the validation of allowed hosts.


FAQ

?

What does the allow_redirect_hosts hook do?

?

How can I add a new host to the allow_redirect_hosts list?

?

Can I authorize several hosts simultaneously?