Skip to content

Instantly share code, notes, and snippets.

@mehrshaddarzi
Last active May 14, 2025 07:49
Show Gist options
  • Save mehrshaddarzi/b768c7f56425de16a4a162aaa27df5cb to your computer and use it in GitHub Desktop.
Save mehrshaddarzi/b768c7f56425de16a4a162aaa27df5cb to your computer and use it in GitHub Desktop.
Block Run PHP File in WordPress Storage Folder (Uploads)
# Block executables
<FilesMatch "\.(php|phtml|php3|php4|php5|pl|py|jsp|asp|html|htm|shtml|sh|cgi|suspected)$">
deny from all
</FilesMatch>
<?php
add_action('generate_rewrite_rules', 'create_htaccess_uploads_dir');
function create_htaccess_uploads_dir(): void
{
$uploads_dir = wp_upload_dir(null, false);
$file = $uploads_dir['basedir'] . '/.htaccess';
if (file_exists($file)) {
@unlink($file);
}
$content = '# Block executables' . "\n";
$content .= '<FilesMatch "\.(php|phtml|php3|php4|php5|pl|py|jsp|asp|html|htm|shtml|sh|cgi|suspected)$">' . "\n";
$content .= 'deny from all' . "\n";
$content .= '</FilesMatch>';
file_put_contents($file, $content);
}
@mehrshaddarzi
Copy link
Author

define('DISALLOW_FILE_EDIT', true);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment