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_allowed_safe_ports filter
Allows you to change and allow external requests for HTTP requests. To use the http_allowed_safe_ports filter, you first need to register it using add_filter. You can write this code in the functions.php of your activated theme or in a custom WordPress Plugin.
We at WePlugins always prefer creating a custom WordPress Plugin when using hooks, so nothing breaks when you update your WordPress Theme in the future.
In the live examples below, we define a function weplugins_modify_http_allowed_safe_ports_defaults which takes 3 parameters and is registered using add_filter. The first parameter http_allowed_safe_ports is the name of the hook, the second parameter weplugins_modify_http_allowed_safe_ports_defaults is the name of the function that needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times, and the last parameter is the number of arguments (if any) to be passed in the registered function.
Sometimes, you have to remove a registered hook, so you can use remove_filter to remove the http_allowed_safe_ports filter.
Example 1: Modify Allowed Ports
Below is an example of how you can use this hook.
function weplugins_modify_http_allowed_safe_ports_defaults($allowed_ports, $host, $url) { // Update the $allowed_ports variable according to your website requirements and return this variable. You can modify the $allowed_ports variable conditionally too if you want. return $allowed_ports; } // add the filter add_filter( "http_allowed_safe_ports", "weplugins_modify_http_allowed_safe_ports_defaults", 10, 3 );
Example 2: Removing a Hook Callback
To remove a hook callback, use the example below.
remove_filter( "http_allowed_safe_ports", "weplugins_modify_http_allowed_safe_ports_defaults", 10, 3 );
Please make sure to provide the same callback function name, priority, and number of arguments when removing the hook callback.
Example 3: Custom Conditional Port Addition
This example demonstrates conditional addition of a custom port.
function weplugins_custom_port_addition($allowed_ports, $host, $url) { if ($host === 'example.com') { $allowed_ports[] = 8080; // Add port 8080 for example.com } return $allowed_ports; } add_filter( "http_allowed_safe_ports", "weplugins_custom_port_addition", 10, 3 );
Contact Us
If you need any customization or are having trouble using this hook, please contact us and our team would be happy to assist you.
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.