Skip to content

Instantly share code, notes, and snippets.

@ofmarconi
Last active June 19, 2022 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ofmarconi/54a66650cbdd479dc4d2633e0dcf5d4f to your computer and use it in GitHub Desktop.
Save ofmarconi/54a66650cbdd479dc4d2633e0dcf5d4f to your computer and use it in GitHub Desktop.
Check if the upload file name contains .gif If it does, refuse to upload it to the media gallery (by askjarvis.io)
function wpse_check_file_type( $file ) {
$filetype = wp_check_filetype( $file['name'] );
$ext = $filetype['ext'];
$type = $filetype['type'];
$proper_filename = $file['name'];
$wp_filetype = wp_check_filetype( $proper_filename, null );
$ext = $wp_filetype['ext'];
$type = $wp_filetype['type'];
$proper_filename = preg_replace('/\.[^.]+$/', '', basename( $file['name'] ) );
$filename = $proper_filename . '.' . $ext;
if ( $type == 'image/gif' ) {
$file['error'] = 'Sorry, GIF files are not allowed.';
return $file;
}
return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'wpse_check_file_type' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment