Removing Prefixes & Suffixes from Media Filenames

tdrayson

When uploading files to WordPress, it’s not uncommon to encounter filenames that have been altered by various tools or processes. One of the main culprits is image optimization tools. These tools frequently append suffixes to the filenames to signify that they have been processed. For instance, an optimized image might have a suffix like “-optimized” or “-compressed”. Switching between optimization tools over time can clutter your media library with suffixes, making file management more confusing.

The following guide will walk you through a snippet that removes specific prefixes and suffixes from filenames during the upload process in WordPress, helping you maintain a clean naming convention.

How to Use the Snippet

Access Your Theme’s File:
Open your WordPress theme’s functions.php file. This can typically be accessed via FTP or through your hosting control panel.

Paste the Snippet:
At the end of the functions.php file, paste the code snippet below.

Members content only

Become a SnippetClub member to gain instant access to this tutorial plus tons more premium content.

Already a member? Log in below.

Login Form

👋🏻 Weekly Tutorial Digest

I send out a weekly newsletter with links to new tutorials written in the last week, you can subscribe below.

Newsletter

🔒I won't send you spam, I promise