I am doing a page speed test and there is a specific domain that keeps causing lag time in page speed. I do not think there is any use for these external files so I would like to omit them during the page load when a user visits the site.
I found this url which is very close to what I am looking for but I do not see the need to create 2 lines of code. block specific external calls in WordPress?
Instead of blocking them all and then whitelisting the ones I want to allow, I would simply like to block the 1 external url which I believe I should only need 1 line of code with WP. Can someone help point me in the right direction?
This is what I have now. define('WP_HTTP_BLOCK_EXTERNAL', true);
Line 2, whitelisting define('WP_ACCESSIBLE_HOSTS', 'site1.com, site2.com');
Thanks for the help!
There's no shorter way (in lines) than using WP_HTTP_BLOCK_EXTERNAL as you already have.
The only other way I can think of is overriding pre_http_request - documentation at https://developer.wordpress.org/reference/hooks/pre_http_request/ - with returning non false value and thus not requesting the external URL response (e.g. "short circuiting"):
add_filter('pre_http_request', function($request) {
if( $request['url'] != 'https://www.site1.com' ) {
return false;
}
return "my replacement content";
})
I first thought you want to block certain external website to access your WordPress website that you could do with .htaccess modification. I'll just leave that answer too (supposing you are using Apache web server):
# BEGIN Block sites
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_REFERER} site1\.com [NC]
RewriteCond %{HTTP_REFERER} site2\.com [NC]
RewriteRule ^(.*)$ - [F]
</IfModule>
# END Block sites
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress