Using WordPress ‘download_url_error_max_body_size’ PHP filter

The download_url_error_max_body_size WordPress PHP filter allows you to modify the maximum error response body size in the download_url() function.

Usage

add_filter('download_url_error_max_body_size', 'my_custom_max_body_size');

function my_custom_max_body_size($size) {
    // Your custom code here
    return $size;
}

Parameters

  • $size (int) – The maximum error response body size. Default is 1 KB.

More information

See WordPress Developer Resources: download_url_error_max_body_size

Examples

Increase maximum error response body size to 5 KB

In this example, we increase the maximum error response body size to 5 KB.

add_filter('download_url_error_max_body_size', 'increase_max_body_size');

function increase_max_body_size($size) {
    return 5 * 1024; // 5 KB
}

Set a custom maximum error response body size based on user role

In this example, we set a custom maximum error response body size based on the user role.

add_filter('download_url_error_max_body_size', 'custom_max_body_size_based_on_role');

function custom_max_body_size_based_on_role($size) {
    $user = wp_get_current_user();
    if (in_array('administrator', $user->roles)) {
        return 10 * 1024; // 10 KB for administrators
    }
    return 2 * 1024; // 2 KB for other users
}

Set a dynamic maximum error response body size based on a custom option

In this example, we set a dynamic maximum error response body size based on a custom option stored in the database.

add_filter('download_url_error_max_body_size', 'dynamic_max_body_size_based_on_option');

function dynamic_max_body_size_based_on_option($size) {
    $custom_size = get_option('my_custom_max_body_size');
    if ($custom_size) {
        return $custom_size;
    }
    return $size;
}

Disable maximum error response body size

In this example, we disable the maximum error response body size.

add_filter('download_url_error_max_body_size', 'disable_max_body_size');

function disable_max_body_size($size) {
    return 0; // No maximum error response body size
}

Multiply the default maximum error response body size by a factor

In this example, we multiply the default maximum error response body size by a factor.

add_filter('download_url_error_max_body_size', 'multiply_max_body_size_by_factor');

function multiply_max_body_size_by_factor($size) {
    $factor = 3;
    return $size * $factor; // Multiply default size by the factor
}