PHP snippet · Media & Files
Auto alt text from the filename
On upload, it fills in empty alt text from the filename, so red-brick-farmhouse.jpg becomes Red brick farmhouse. The alt text you wrote is never touched.
About this php snippet
Missing alt text is the most common accessibility failure on the average WordPress site, and it fails silently. Nobody notices except the people who need it most, plus every automated audit you will ever run.
On upload, this turns the filename into a readable first draft, so the library stops filling up with blank alt fields while nobody is watching.
What it does
- Runs on upload, on images only, and only when the alt field is empty
- Turns hyphens, underscores, and dots into spaces and capitalises the first word, so red-brick-farmhouse_02.jpg becomes Red brick farmhouse 02
- Strips a trailing size suffix such as -1024x768, so a resized file does not carry the dimensions into its alt text
- Falls back to the attachment title if the filename cannot be read
Good to know
- Alt text you wrote yourself is never overwritten. It only ever fills an empty field.
- A filename is a starting point, not a description. Treat this as a floor that stops the site shipping blank alts, then improve the ones that carry meaning.
- It applies to new uploads. Images already in the library are left as they are.
- Name your files properly before uploading and this does most of the work for you



