2

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!

Rookie Recruits
  • 93
  • 3
  • 18
  • Are you seeing the domain in a page speed report? If so, that isn’t WordPress, that is HTML, JS, CSS and the like. Although WordPress might be used to generate that, it might also just be static resources. Use a tool such as [Request Map](https://requestmap.herokuapp.com/) to find what is ultimately calling your domain, then try grepping for that – Chris Haas Jun 16 '20 at 02:19
  • ok, thanks for the feedback @ChrisHaas and will take a look at that tool you recommended. – Rookie Recruits Jun 17 '20 at 13:23
  • isn't that a good job for a Content Security Policy? – Tobias Jun 18 '20 at 09:14
  • @Tobias IDK, maybe but it seems the process takes much longer than if I could simply block the ones I don't want to call out externally. Maybe there's something I am not aware of in that context. What I was concerned with is creating more processes than to just have the system do the one thing I need it to do. There has to be a way to just block the one external resource that I want to block. No? – Rookie Recruits Jun 22 '20 at 12:20

1 Answers1

1

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
Matt Sergej Rinc
  • 565
  • 3
  • 11
  • I don't think these will work exactly for what I am looking for however, I do see some uses for this on some of my current projects so I appreciate the information. I hope they pass you the bounty. – Rookie Recruits Jun 26 '20 at 21:37