Only allow the file types your site actually needs, such as images, PDFs and video, in the Media Library.
add_filter( 'upload_mimes', function ( array $mimes ): array {
// Keys must match WordPress's own list exactly.
$allowed = [
'jpg|jpeg|jpe',
'png',
'gif',
'webp',
'avif',
'pdf',
'mp4|m4v',
'mp3|m4a|m4b',
];
return array_intersect_key( $mimes, array_flip( $allowed ) );
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
WordPress lets users upload dozens of file types, including spreadsheets, archives and audio formats most sites never use. Every extra type is more for an attacker to work with if an account is compromised. This snippet allows only the types you list.
The upload_mimes filter returns the allowed extensions and their MIME types. The snippet keeps only the entries whose keys are in $allowed. WordPress rejects anything else with “Sorry, you are not allowed to upload this file type.”
The keys must match WordPress’s own exactly, including the grouped ones like jpg|jpeg|jpe. You can see the full list with print_r( get_allowed_mime_types() );.
doc|docx, xla|xls|xlt|xlw or zip if your site needs them.Try uploading a .zip file to the Media Library. It should be refused. A .jpg should still upload.