The image_get_intermediate_size WordPress PHP Filter allows you to modify the output of the image_get_intermediate_size() function.
Usage
add_filter('image_get_intermediate_size', 'my_custom_function', 10, 3);
function my_custom_function($data, $post_id, $size) {
// your custom code here
return $data;
}
Parameters
- $data (array) – Array of file relative path, width, and height on success. May also include file absolute path and URL.
- $post_id (int) – The ID of the image attachment.
- $size (string|int[]) – Requested image size. Can be any registered image size name, or an array of width and height values in pixels (in that order).
More information
See WordPress Developer Resources: image_get_intermediate_size
Examples
Add custom image size
Modify the image size by adding custom width and height.
add_filter('image_get_intermediate_size', 'my_custom_image_size', 10, 3);
function my_custom_image_size($data, $post_id, $size) {
if ($size == 'custom_size') {
$data['width'] = 500;
$data['height'] = 300;
}
return $data;
}
Modify image URL
Change the image URL for a specific size.
add_filter('image_get_intermediate_size', 'modify_image_url', 10, 3);
function modify_image_url($data, $post_id, $size) {
if ($size == 'thumbnail') {
$data['url'] = 'https://example.com/new-image-url.jpg';
}
return $data;
}
Limit maximum image width
Prevent the image width from exceeding a specific value.
add_filter('image_get_intermediate_size', 'limit_image_width', 10, 3);
function limit_image_width($data, $post_id, $size) {
$max_width = 600;
if ($data['width'] > $max_width) {
$data['width'] = $max_width;
}
return $data;
}
Set a default image size
Set a default image size when the requested size is not available.
add_filter('image_get_intermediate_size', 'default_image_size', 10, 3);
function default_image_size($data, $post_id, $size) {
if (empty($data)) {
$data['width'] = 400;
$data['height'] = 300;
}
return $data;
}
Modify image file path
Change the image file path for a specific size.
add_filter('image_get_intermediate_size', 'modify_image_file_path', 10, 3);
function modify_image_file_path($data, $post_id, $size) {
if ($size == 'thumbnail') {
$data['file'] = 'path/to/new-image.jpg';
}
return $data;
}