Skip to content

Instantly share code, notes, and snippets.

@robwent
Created March 17, 2019 16:29
Show Gist options
  • Save robwent/acda1609cc3cbcb6668aacc3a8973aab to your computer and use it in GitHub Desktop.
Save robwent/acda1609cc3cbcb6668aacc3a8973aab to your computer and use it in GitHub Desktop.
Stops WordPress uploading images if the filename contains certain words.
<?php
/*
Plugin Name: Check Upload Filename
Plugin URI: https://www.robertwent.com/
Description: Prevents image uploads by filename
Version: 1.0
Author: Robert Went
*/
add_filter('wp_handle_upload_prefilter', 'check_for_lazy_people_uploads' );
function check_for_lazy_people_uploads( $file ){
$bad_words = array(
'screenshot',
'unsplash',
'shutterstock',
'istock',
'img_' //Camara phones
);
//Check if the filename contains any matches
$name = strtolower($file['name']);
foreach ( $bad_words as $word ) {
if ( stripos( $name, $word ) !== false ) {
//Match found, so stop the upload and show an error message
$file['error'] = __( 'Please take a couple of seconds to optimise your image filename before uploading! Filenames should not include: ' ) . $word;
}
}
return $file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment