This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
http_request_host_is_external filter
Ever wondered how to manage external HTTP requests in WordPress? The http_request_host_is_external filter is your go-to hook. It lets you allow or restrict external requests for the HTTP request.
To use the http_request_host_is_external filter, you first need to register it with add_filter. You can place this code in the functions.php file of your active theme or, preferably, in a custom WordPress Plugin. At WePlugins, we always recommend creating a custom plugin to ensure your changes remain intact even after theme updates.
Check out the example below where we define a function weplugins_modify_http_request_host_is_external_defaults which takes three parameters and registers it using add_filter. The first parameter http_request_host_is_external is the hook name, the second parameter weplugins_modify_http_request_host_is_external_defaults is the callback function, the third parameter is the priority (useful if the hook is used multiple times), and the last parameter is the number of arguments to be passed to the callback function.
Sometimes, you might need to remove a registered hook, and for that, you can use remove_filter to remove the http_request_host_is_external filter.
Parameters
- $external: (bool) Whether the HTTP request is external or not.
- $host: (string) Host name of the requested URL.
- $url: (string) Requested URL.
Below are the 3 parameters required to use this hook:
Live Example 1: Basic Usage
Below is an example of how you can use this hook.
function weplugins_modify_http_request_host_is_external_defaults($external, $host, $url) { // Update the $external variable according to your website requirements and return this variable. // You can modify the $external variable conditionally too if you want. return $external; } // add the filter add_filter( "http_request_host_is_external", "weplugins_modify_http_request_host_is_external_defaults", 10, 3 );
Live Example 2: Conditional Modification
Here is an example of modifying the $external variable based on specific conditions.
function weplugins_modify_http_request_host_is_external_conditionally($external, $host, $url) { if ($host == 'example.com') { $external = false; } return $external; } add_filter( "http_request_host_is_external", "weplugins_modify_http_request_host_is_external_conditionally", 10, 3 );
Live Example 3: Removing the Hook
To remove a hook callback, use the example below.
remove_filter( "http_request_host_is_external", "weplugins_modify_http_request_host_is_external_defaults", 10, 3 );
Please make sure to provide the same callback function name, priority, and number of arguments while removing the hook callback.
If you’re having any trouble using this hook, please contact us for customization.
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.