Warning: DOMDocument::loadHTML(): Unexpected end tag : span in Entity, line: 15 in /srv/users/serverpilot/apps/robertwentcom/public/wp-content/themes/rw/inc/extras.php on line 119
Are you continually telling people to optimise their image filenames before uploading them to a website?
Do staff members keep assuring you that they will optimise their filenames in the future whilst having no intention of doing so?
Have they said "I'll go back and do it later" rather than taking 2 seconds to do the job properly in the first place?
Then it's time to start blocking their uploads so you don't have to check up on them like a child :)
WordPress has a filter which runs early on in the upload process which we can use to check the filename of the upload: wp_handle_upload_prefilter.
In the following snippet, we define an array of words which we don't want the filename to contain.
In my case, I want to stop people uploading images directly from stock image websites, so I include 'unsplash', 'shutterstock' and other strings which appear in the filenames of the stock sites I know the company use.
I also include 'screenshot' to prevent uploads from screenshotting programs which produce images like Screenshot-2017-05-10-17.23.07.png.
And finally, I include 'img_' to prevent the direct upload of camera phone images.
As the filter passes the file information variable, we can then loop through the array of words and check if the filename includes any matches.
If there is no match, we just return the original $file variable.
If there is a match, we add an error message to the array, which will halt the upload and show the message to the user.
The message includes the string match so that the user knows why the upload was prevented.
We can stop the plugin from being deactivated in the admin area by adding it as a 'Must Use' plugin by adding the file to the /wp-uploads/mu-plugins/ folder (Plugins in this folder must be single files and not added in their own directory).
If the folder doesn't exist, you can create it, drop the file in there and it will run without showing in the admin plugin list.
Now when someone uploads an image without optimising it first, they will see an error message like this:
Luckily there are plugins out there already which will automatically add alt text for you.