In this guide
1. Block PHP execution in the uploads folder
The most important step. Even if an attacker manages to upload a PHP file, it won’t run.
Apache: create a file named .htaccess inside wp-content/uploads/ with:
<FilesMatch "\.(php|phtml|phar|php[0-9])$">
Require all denied
</FilesMatch>
Nginx: ask your host to add a rule that denies .php files under /wp-content/uploads/.
Many security plugins (Solid Security, AIOS and others) can apply this setting for you. If your site is behind Cloudflare, a firewall rule can block these requests too. See Cloudflare firewall rules for WordPress.
2. Allow only the file types you need
WordPress already restricts uploads to common media types. Don’t widen that list unless you must, and be careful with plugins that add “allow any file type” options. Never allow .php, .phtml, .html or .js uploads from untrusted users.
3. Check what’s already in uploads
Find PHP files that shouldn’t be there:
find wp-content/uploads -type f \( -name "*.php" -o -name "*.phtml" -o -name "*.phar" \) -not -name "index.php"
Also look for files whose extension doesn’t match their content, such as a .jpg or .ico that’s actually code. See Is this file malware?
4. Protect private files
Files in wp-content/uploads are public by default: anyone with the link can open them. For private documents (invoices, member downloads, form uploads):
- store them outside the web root, or use a plugin that serves them only to authorized users;
- make sure directory listing is off (see directory listing);
- never leave backups or database exports in uploads.
5. Limit who can upload
Only give upload rights to roles that need them, review users regularly, and turn on two-factor authentication. A taken-over editor account can upload anything an editor can.
From my cleanup work: in a large share of the cleanups I do, at least one backdoor sits somewhere in wp-content/uploads, often in an old year folder like uploads/2021/03/ with an innocent name. Blocking PHP in uploads wouldn’t have stopped the original hack, but it would have made that backdoor useless.