How To: Disable/Block External HTTP Requests In WordPress

Posted by Artem Russakovskii on November 16th, 2011 in PHP, Wordpress

One thing that's been continuously annoying me when doing WordPress development on the go is when something somewhere inside WordPress decides to send requests to external urls when I don't even have Internet connection or it's slow and flaky (tethering, slow Wi-Fi, etc). This results in random lag when loading pages, especially if I haven't opened my dev WordPress instance for a long time.

Turns out there's an easy and undocumented (other than in code) solution. To block external HTTP requests right in WordPress's core itself, open up wp-config.php and add WP_HTTP_BLOCK_EXTERNAL like so:

define('WP_HTTP_BLOCK_EXTERNAL', true);

Whenever this variable is present, external requests will be ignored, unless you specify your own comma-separated whitelist of domains using WP_ACCESSIBLE_HOSTS like so:

define('WP_ACCESSIBLE_HOSTS', 'site1.com, site2.com');

You can examine relevant core code here and the relevant bug reports here and here.

Happy developing!

● ● ●
Artem Russakovskii is a San Francisco programmer and blogger. Follow Artem on Twitter (@ArtemR) or subscribe to the RSS feed.

In the meantime, if you found this article useful, feel free to buy me a cup of coffee below.

  • Baf

    Is it usefull only for development, or for something else?

    • If you feel paranoid about plugins and all kinds of things accessing outside services, you can finetune what they can access there. But that's, of course, if they plugins use the WP HTTP API in the first place (most should).

  • Valkyria

    Thank you! WordPress was incredibly slow in local unitl I added this. Thanks again

  • Pingback: Poofy hair can certainly major problem for a lot of women()

  • With no internet connection its still slow and shows error about not being able to connect to wordpress etc. But it helped a little bit.

  • Naeem Mirza

    Where do we place this code in wordpress?

  • Pingback: Macpro()

  • You might also like the Snitch plugin. This will not only show which hosts but also the amount of time needed for the connection!

  • Bo Hansen

    Thanks, this helped me on an internal site where the admin pages were slow.