The attachment_max_dims WordPress PHP filter allows you to modify the maximum dimensions for an uploaded attachment.
Usage
add_filter('attachment_max_dims', 'custom_attachment_max_dims', 10, 3); function custom_attachment_max_dims($max_dims, $width, $height) { // your custom code here return $max_dims; }
Parameters
$max_dims
(array): An array containing the maximum width and height for the attachment in the formatarray($width, $height)
.$width
(int): The original width of the attachment.$height
(int): The original height of the attachment.
More information
See WordPress Developer Resources: attachment_max_dims
Examples
Limit attachment dimensions to 800×600
This example sets the maximum dimensions for an uploaded attachment to 800 pixels wide and 600 pixels tall.
add_filter('attachment_max_dims', 'limit_attachment_dimensions', 10, 3); function limit_attachment_dimensions($max_dims, $width, $height) { $max_dims = array(800, 600); return $max_dims; }
Scale dimensions proportionally
This example scales the attachment dimensions proportionally to fit within a maximum width of 1200 pixels.
add_filter('attachment_max_dims', 'scale_attachment_dimensions', 10, 3); function scale_attachment_dimensions($max_dims, $width, $height) { $max_width = 1200; if ($width > $max_width) { $ratio = $max_width / $width; $width = $max_width; $height = $height * $ratio; } $max_dims = array($width, $height); return $max_dims; }
Set dimensions based on orientation
This example sets the maximum dimensions based on the attachment’s orientation (portrait or landscape).
add_filter('attachment_max_dims', 'orientation_based_dimensions', 10, 3); function orientation_based_dimensions($max_dims, $width, $height) { if ($width > $height) { // Landscape $max_dims = array(1200, 800); } else { // Portrait $max_dims = array(800, 1200); } return $max_dims; }
Prevent large attachments
This example prevents attachments larger than 1600×1200 pixels from being uploaded.
add_filter('attachment_max_dims', 'prevent_large_attachments', 10, 3); function prevent_large_attachments($max_dims, $width, $height) { if ($width > 1600 || $height > 1200) { return false; } return $max_dims; }
Enforce square attachments
This example enforces that all uploaded attachments must be square.
add_filter('attachment_max_dims', 'enforce_square_attachments', 10, 3); function enforce_square_attachments($max_dims, $width, $height) { if ($width !== $height) { return false; } return $max_dims; }