Using WordPress ‘http_request_args’ PHP filter

The http_request_args WordPress PHP Filter allows you to modify the arguments used in an HTTP request.

Usage

add_filter('http_request_args', 'your_custom_function', 10, 2);

function your_custom_function($parsed_args, $url) {
    // your custom code here
    return $parsed_args;
}

Parameters

  • $parsed_args (array): An array of HTTP request arguments.
  • $url (string): The request URL.

More information

See WordPress Developer Resources: http_request_args

Examples

Modify user-agent in HTTP request

Change the user-agent for the request.

add_filter('http_request_args', 'change_user_agent', 10, 2);

function change_user_agent($parsed_args, $url) {
    $parsed_args['user-agent'] = 'Your Custom User Agent';
    return $parsed_args;
}

Increase HTTP request timeout

Increase the timeout for a specific request.

add_filter('http_request_args', 'increase_request_timeout', 10, 2);

function increase_request_timeout($parsed_args, $url) {
    if (strpos($url, 'example.com') !== false) {
        $parsed_args['timeout'] = 60;
    }
    return $parsed_args;
}

Disable SSL verification

Disable SSL verification for a specific request.

add_filter('http_request_args', 'disable_ssl_verification', 10, 2);

function disable_ssl_verification($parsed_args, $url) {
    if (strpos($url, 'example.com') !== false) {
        $parsed_args['sslverify'] = false;
    }
    return $parsed_args;
}

Add custom header to HTTP request

Add a custom header to the HTTP request.

add_filter('http_request_args', 'add_custom_header', 10, 2);

function add_custom_header($parsed_args, $url) {
    $parsed_args['headers']['X-Custom-Header'] = 'Your Custom Value';
    return $parsed_args;
}

Block HTTP requests to a specific domain

Prevent requests to a specific domain.

add_filter('http_request_args', 'block_specific_domain', 10, 2);

function block_specific_domain($parsed_args, $url) {
    if (strpos($url, 'baddomain.com') !== false) {
        $parsed_args['blocking'] = false;
    }
    return $parsed_args;
}