4

How To: Disable/Block External HTTP Requests In WordPress


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

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:

1
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:

1
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, blogger, and future millionaire (that last part is in the works). 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.



Share
  • Baf

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

    • http://beerpla.net Artem Russakovskii

      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