Using WordPress ‘http_request_redirection_count’ PHP filter

The http_request_redirection_count WordPress PHP Filter allows you to modify the number of redirects allowed during an HTTP request.

Usage

add_filter('http_request_redirection_count', 'modify_http_request_redirection_count', 10, 2);

function modify_http_request_redirection_count($redirect_count, $url) {
  // your custom code here
  return $redirect_count;
}

Parameters

  • $redirect_count (int) – Number of redirects allowed. Default is 5.
  • $url (string) – The request URL.

More information

See WordPress Developer Resources: http_request_redirection_count

Examples

Increase Redirect Count

To increase the allowed redirect count to 10:

function increase_redirect_count($redirect_count, $url) {
  $redirect_count = 10;
  return $redirect_count;
}
add_filter('http_request_redirection_count', 'increase_redirect_count', 10, 2);

Decrease Redirect Count

To decrease the allowed redirect count to 3:

function decrease_redirect_count($redirect_count, $url) {
  $redirect_count = 3;
  return $redirect_count;
}
add_filter('http_request_redirection_count', 'decrease_redirect_count', 10, 2);

Disable Redirects

To disable redirects completely:

function disable_redirects($redirect_count, $url) {
  $redirect_count = 0;
  return $redirect_count;
}
add_filter('http_request_redirection_count', 'disable_redirects', 10, 2);

Increase Redirect Count for Specific Domain

To increase the redirect count to 10 for a specific domain:

function increase_redirect_count_for_specific_domain($redirect_count, $url) {
  if (strpos($url, 'example.com') !== false) {
    $redirect_count = 10;
  }
  return $redirect_count;
}
add_filter('http_request_redirection_count', 'increase_redirect_count_for_specific_domain', 10, 2);

Redirect Count Based on URL Length

To set the redirect count based on the length of the URL:

function redirect_count_based_on_url_length($redirect_count, $url) {
  $url_length = strlen($url);
  if ($url_length > 100) {
    $redirect_count = 2;
  } elseif ($url_length > 50) {
    $redirect_count = 4;
  } else {
    $redirect_count = 6;
  }
  return $redirect_count;
}
add_filter('http_request_redirection_count', 'redirect_count_based_on_url_length', 10, 2);