PortGate

Resources · Security guides · Security guide

Stop PHP from running inside WordPress uploads

Uploads should hold images and documents, never code. Deny PHP execution there in nginx or Apache, block xmlrpc.php if unused, and turn off the dashboard file editor.

Check command

find wp-content/uploads -type f -name '*.php'

Copied.

Most WordPress takeovers end the same way: a vulnerable plugin or a stolen login writes a PHP file into wp-content/uploads, and the attacker requests it. Uploads only ever need images, PDFs and media. If the web server refuses to execute PHP there, that last step fails even when the plugin bug is not yet patched.

Check for PHP files in uploads now

cd /var/www/example.com
find wp-content/uploads -type f -name '*.php'
find wp-content/uploads -type f -name '*.php*' -o -type f -name '*.phtml'

A clean site prints nothing. Do not delete a file you find before you look at it: note its timestamp and compare it with your access logs to see how it arrived.

nginx

Place this inside the server block, before the general location ~ \.php$ block, because nginx uses the first matching regex location:

location ~* ^/wp-content/uploads/.*\.(?:php[0-9]?|phtml|phar)$ {
    deny all;
}

location = /xmlrpc.php {
    deny all;
}

Test and reload:

sudo nginx -t && sudo systemctl reload nginx
curl -s -o /dev/null -w '%{http_code}\n' https://example.com/wp-content/uploads/test.php

The answer should be 403. Keep the xmlrpc.php rule only if nothing you use needs XML-RPC: Jetpack and some mobile app workflows do.

Apache

Create wp-content/uploads/.htaccess:

<FilesMatch "\.(?:php[0-9]?|phtml|phar)$">
    Require all denied
</FilesMatch>

This only works when the vhost allows overrides (AllowOverride All or at least FileInfo and AuthConfig). The vhost config is the more reliable place for the same rule.

Turn off the dashboard file editor

Add to wp-config.php, above the "That's all, stop editing" line:

define('DISALLOW_FILE_EDIT', true);

A stolen admin session can no longer paste code into a theme file from the browser.

What this does not fix

A vulnerable plugin is still vulnerable. This rule stops a new PHP file in uploads from running; it does not stop a plugin bug that runs code directly, or a file written somewhere else. Keep plugins updated, remove unused ones, and watch for new PHP files across the whole web root.