Skip to content

Strip image metadata on upload in WordPress

Blog  ✺  WordPress  ✺  Code Snippet
Strip Metadata from WordPress Image Uploads. Image generated with Dall E3

For a client project I'm working on the client wanted to ensure that all geo data and unnecessary metadata was stripped from their images when they upload them to the WordPress media library.


How WordPress VIP does it?

I first came across a solution that WordPress VIP offers their clients using the Gmagick php extension. Since my client will be using Imagick instead, I rewrote the functions to fit my needs. They offer two solutions, one is by adding a URL parameter to all images, but they suggest using code instead to strip metadata on upload. Learn more about how WordPress VIP handles this:

Strip image metadata on upload · WordPress VIP Documentation
By default, image metadata is not stripped from uploaded images. The GET ?strip=all|info|color query parameter string can be added to images served by VIP’s File System in order to remove JPEG image Exif, IPTC, comment, and color data from the output image in the browser. A more effective method to…

What image metadata?

We want to remove JPEG image Exif, IPTC, comment, and color data from the images uploaded to the WordPress media library.

In the code example below, we preserve the image file’s ICC color profile data (icc) but strip all other metadata from images on upload. This code example can be modified to preserve other types of profile data, or to strip all metadata entirely upon file upload.


Code Snippet to strip image metadata

This code snippet can be added to a theme’s functions.php, or as a separate plugin.

That's it. Once in your theme or plugin, all future uploads will be stripped of their unused metadata. Please make sure this is compliant with any images you licence from third parties.


Before you go, let me know on twitter if you like this type of content? Feel free to sign up to my newsletter for more.

Comments

You might like

Code Snippet: Remove Jetpack Related Posts from Woo Testimonials

WordPress  ✺  WooCommerce  ✺  Code Snippet

Keeping Calm and Productive with a "Loud Mind"

Personal  ✺  Opinion