Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use http_request_host_is_external filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
September 27, 2022
5 minutes read

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

    Below are the 3 parameters required to use this hook:

  • $external: (bool) Whether the HTTP request is external or not.
  • $host: (string) Host name of the requested URL.
  • $url: (string) Requested URL.

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.

Access Premium WordPress Plugins

If you’re having any trouble using this hook, please contact us for customization.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

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.