Skip to content

Instantly share code, notes, and snippets.

@samuelaguilera
Last active September 27, 2023 09:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save samuelaguilera/5cac9863b54099ee85dfd5419799f469 to your computer and use it in GitHub Desktop.
Save samuelaguilera/5cac9863b54099ee85dfd5419799f469 to your computer and use it in GitHub Desktop.
Custom timeout values for HTTP requests in WP
<?php
// NOTE: THE CODE TO COPY/PASTE STARTS *BELOW* THIS LINE
/**
* The snippets below use 30 seconds for testing. If they help, try lowering the value.
* If you are still getting cURL timeouts after raising the timeout to 30 seconds, your host must investigate the issue further.
*/
// Setting a custom timeout value for cURL. Using a high value for priority to ensure the function runs after any other added to the same action hook.
add_action('http_api_curl', 'sar_custom_curl_timeout', 9999, 1);
function sar_custom_curl_timeout( $handle ){
curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 30 ); // 30 seconds.
curl_setopt( $handle, CURLOPT_TIMEOUT, 30 ); // 30 seconds.
}
// Setting custom timeout for the HTTP request
add_filter( 'http_request_timeout', 'sar_custom_http_request_timeout', 9999 );
function sar_custom_http_request_timeout( $timeout_value ) {
return 30; // 30 seconds.
}
// Setting custom timeout in HTTP request args
add_filter('http_request_args', 'sar_custom_http_request_args', 9999, 1);
function sar_custom_http_request_args( $r ){
$r['timeout'] = 30; // 30 seconds.
return $r;
}
@nayanchy
Copy link

Could you please tell me where to paste those codes?

@pavlo-bondarchuk
Copy link

in functions.php

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