Skip to content

Instantly share code, notes, and snippets.

@goblindegook
Created February 26, 2012 23:15
Show Gist options
  • Save goblindegook/1919587 to your computer and use it in GitHub Desktop.
Save goblindegook/1919587 to your computer and use it in GitHub Desktop.
Adjust WordPress HTTP request timeout
<?php
function my_http_request_args ( $r )
{
$r['timeout'] = 15; # new timeout
return $r;
}
add_filter( 'http_request_args', 'my_http_request_args', 100, 1 );
function my_http_api_curl ( $handle )
{
curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 15 ); # new timeout
curl_setopt( $handle, CURLOPT_TIMEOUT, 15 ); # new timeout
}
add_action( 'http_api_curl', 'my_http_api_curl', 100, 1 );
?>
@raamdev
Copy link

raamdev commented Dec 30, 2015

The new way of doing this using the http_request_timeout filter:

<?php
/**
 * Extend http timeout duration to 15 seconds
 * 
 * @param int $timeout The timeout duration in seconds. Default is 5.
 *
 * @return int The filtered timeout duration in seconds.
 */
function __extend_http_request_timeout( $timeout ) {
    return 15; // seconds
}
add_filter( 'http_request_timeout', '__extend_http_request_timeout' );

@AbeCole
Copy link

AbeCole commented May 16, 2019

@raamdev code has incorrectly spelt function name, "__extend_http_reqest_timeout" is missing the "u" in "request".

Correct snippet is:

<?php
/**
 * Extend http timeout duration to 15 seconds
 * 
 * @param int $timeout The timeout duration in seconds. Default is 5.
 *
 * @return int The filtered timeout duration in seconds.
 */
function __extend_http_request_timeout( $timeout ) {
    return 15; // seconds
}
add_filter( 'http_request_timeout', '__extend_http_request_timeout' );

@raamdev
Copy link

raamdev commented May 16, 2019

@AbeCole thanks for catching that! I updated my comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment