Created
March 17, 2019 16:29
-
-
Save robwent/acda1609cc3cbcb6668aacc3a8973aab to your computer and use it in GitHub Desktop.
Stops WordPress uploading images if the filename contains certain words.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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