Snippet to Block Outgoing HTTP Requests (Single or Multiple URLs)
-
After a decent amount of research and testing, I would like to share a method that will allow you to block unwanted outgoing HTTP requests for either single or multiple URLs.
As most of you know, outgoing HTTP requests are implemented by WordPress, Themes, and Plugins to perform many functions. Most of them relate to checking your website to determine if any updates are needed. Others, simply check your website for license validation or to simply perform miscellaneous theme or plugin tests which, quite frankly, can slow down your website.
To help minimize these effects (i.e., slowing down your website), follow the method provided below.
Method:
(1) Install and activate Snitch.
(2) Using Snitch, determine which URL(s) are making outgoing HTTP requests and potentially slowing down your website.
(3) Add the snippet provided below to your functions.php file – or – install and activate the plugin “Code Snippets.” I personally recommend this plugin over adding the code to your functions.php file.
Code:
add_filter( 'pre_http_request', function ( $bFalse, $aReqParams, $sUrl ) { if ( strpos( $sUrl, '//DOMAINURL1/PATH1') ) { $bFalse = null; } return $bFalse; if ( strpos( $sUrl, '//DOMAINURL2/PATH2') ) { $bFalse = null; } return $bFalse; }, PHP_INT_MAX, 3 );
Where DOMAINURL1/PATH1, DOMAINURL2/PATH2 are the URLs/paths obtained from Snitch. If you need to add more URLs, simply repeat the code that starts with “if …” and ends with “… return $bFalse;”
If you know how to simplify the above code, please let us know!
I implemented the above and our website loading time decreased by .2 – .9 seconds.
Cheers!
- The topic ‘Snippet to Block Outgoing HTTP Requests (Single or Multiple URLs)’ is closed to new replies.