The image_strip_meta WordPress PHP Filter allows you to control whether metadata is stripped from images during resizing using the Imagick editor.
Usage
add_filter('image_strip_meta', 'your_custom_function'); function your_custom_function($strip_meta) { // your custom code here return $strip_meta; }
Parameters
$strip_meta
(bool) – Whether to strip image metadata during resizing. Default is true.
More information
See WordPress Developer Resources: image_strip_meta
Examples
Preserve Metadata
Keep the metadata intact while resizing images using the Imagick editor.
add_filter('image_strip_meta', 'preserve_image_metadata'); function preserve_image_metadata($strip_meta) { // Set strip_meta to false to preserve metadata $strip_meta = false; return $strip_meta; }
Strip Metadata for Large Images
Remove metadata from images larger than 1MB to save space.
add_filter('image_strip_meta', 'strip_large_image_metadata', 10, 2); function strip_large_image_metadata($strip_meta, $file) { // Check if the file size is larger than 1MB if (filesize($file) > 1048576) { // Set strip_meta to true to remove metadata $strip_meta = true; } return $strip_meta; }
Strip Metadata Based on File Type
Remove metadata only from JPEG images.
add_filter('image_strip_meta', 'strip_jpeg_image_metadata', 10, 2); function strip_jpeg_image_metadata($strip_meta, $file) { // Check if the file is a JPEG image if (pathinfo($file, PATHINFO_EXTENSION) === 'jpg') { // Set strip_meta to true to remove metadata $strip_meta = true; } return $strip_meta; }
Strip Metadata for Specific User Role
Remove metadata from images uploaded by users with the ‘editor’ role.
add_filter('image_strip_meta', 'strip_metadata_for_editors', 10, 2); function strip_metadata_for_editors($strip_meta, $file) { // Get the current user $current_user = wp_get_current_user(); // Check if the user has the 'editor' role if (in_array('editor', $current_user->roles)) { // Set strip_meta to true to remove metadata $strip_meta = true; } return $strip_meta; }
Strip Metadata Based on Custom Field Value
Remove metadata from images if a custom field value is set to ‘yes’.
add_filter('image_strip_meta', 'strip_metadata_based_on_custom_field', 10, 2); function strip_metadata_based_on_custom_field($strip_meta, $file) { // Get the post ID from the attachment $post_id = get_post($file)->post_parent; // Check if the custom field value is 'yes' if (get_post_meta($post_id, 'strip_metadata', true) === 'yes') { // Set strip_meta to true to remove metadata $strip_meta = true; } return $strip_meta; }