Using WordPress ‘attachment_thumbnail_args’ PHP filter

The attachment_thumbnail_args WordPress PHP filter allows you to modify the parameters used when creating attachment thumbnails.

Usage

add_filter('attachment_thumbnail_args', 'your_custom_function', 10, 3);

function your_custom_function($image_attachment, $metadata, $uploaded) {
    // your custom code here
    return $image_attachment;
}

Parameters

  • $image_attachment (array) – An array of parameters to create the thumbnail.
  • $metadata (array) – Current attachment metadata.
  • $uploaded (array) – Information about the newly-uploaded file, including:
    • file (string) – Filename of the newly-uploaded file.
    • url (string) – URL of the uploaded file.
    • type (string) – File type.

More information

See WordPress Developer Resources: attachment_thumbnail_args

Examples

Change the thumbnail crop position

Adjust the crop position to the top-left corner when creating attachment thumbnails:

add_filter('attachment_thumbnail_args', 'change_thumbnail_crop_position', 10, 3);

function change_thumbnail_crop_position($image_attachment, $metadata, $uploaded) {
    $image_attachment['crop_position'] = 'left,top';
    return $image_attachment;
}

Set custom thumbnail dimensions

Define custom dimensions for attachment thumbnails:

add_filter('attachment_thumbnail_args', 'set_custom_thumbnail_dimensions', 10, 3);

function set_custom_thumbnail_dimensions($image_attachment, $metadata, $uploaded) {
    $image_attachment['width'] = 150;
    $image_attachment['height'] = 100;
    return $image_attachment;
}

Disable cropping for attachment thumbnails

Disable cropping for attachment thumbnails:

add_filter('attachment_thumbnail_args', 'disable_thumbnail_cropping', 10, 3);

function disable_thumbnail_cropping($image_attachment, $metadata, $uploaded) {
    $image_attachment['crop'] = false;
    return $image_attachment;
}

Set JPEG compression level

Change the JPEG compression level when creating attachment thumbnails:

add_filter('attachment_thumbnail_args', 'set_jpeg_compression_level', 10, 3);

function set_jpeg_compression_level($image_attachment, $metadata, $uploaded) {
    $image_attachment['quality'] = 75;
    return $image_attachment;
}

Apply a filter to the thumbnail image

Apply a custom filter to the thumbnail image before saving:

add_filter('attachment_thumbnail_args', 'apply_custom_filter_to_thumbnail', 10, 3);

function apply_custom_filter_to_thumbnail($image_attachment, $metadata, $uploaded) {
    $image_attachment['apply_filters'] = 'your_custom_image_filter';
    return $image_attachment;
}