How-to Fix WP HTTP Error: name lookup timed out

This blog and the Snowferno site both run WordPress on separate but identically-equipped Lunarpages servers. When I went to apply today’s WordPress 2.8.4 upgrade, one install was giving me “WP HTTP Error: name lookup timed out” messages and no upgrade button, while the other worked just fine. There are some workarounds detailed on the WordPress support site, but I wasn’t happy having to disable cURL on one host and not the other. It had to be something else…

The problem for me was caused by lines 1276-1277 of the wp_Http_Curl request() function in wp-includes/http.php:

curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, $r['timeout'] );
curl_setopt( $handle, CURLOPT_TIMEOUT, $r['timeout'] );

I commented those lines out, added the following line of debug code at line 1325 after the curl_exec call, and tested the cURL transport using Core Control:

print_r( curl_getinfo( $handle ) );

I found that my requests were simply taking longer than the timeout provided. I don’t know quite why, except to guess that one server must be just more swamped than the other.

I fixed it by building myself a little plugin that adds an action for the http_api_curl action called on line 1315.

After enabling this fix, WP showed the 2.8.4 upgrade button on the Dashboard page, but the WordPress Development Blog and Plugins RSS feed boxes still showed timeout errors. So, I looked further and found a filter that catches and can modify $r['timeout'] in the wp_Http request() method on line 237.

Here is my barebones plugin code, which overrides all timeouts to a massive 15 seconds:

//adjustments to wp-includes/http.php timeout values to workaround slow server responses
add_filter( 'http_request_args', 'bal_http_request_args', 100, 1 );
function bal_http_request_args( $r ) //called on line 237
{
    $r['timeout'] = 15;
    return $r;
}

add_action( 'http_api_curl', 'bal_http_api_curl', 100, 1 );
function bal_http_api_curl( $handle ) //called on line 1315
{
    curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 15 );
    curl_setopt( $handle, CURLOPT_TIMEOUT, 15 );
}

I recommend you make sure this does not cause any other issues for your WP install. If you know how to make this code into a plugin, then you are capable enough to be responsible for any adverse fallout. :)

I only know this solved my immediate problem while still maintaining cURL functionality.