607 words
3 minutes
๐Ÿ” Ultimate Guide to Securing Your WordPress Site

In this post, Iโ€™ll walk through essential best practices to secure your WordPress installation, protect sensitive files, and monitor for changes or breaches. Weโ€™ll also include advanced server-level hardening tips that I used in the industry.


โœ… 1. Disable File Editing from wp-admin#

By default, WordPress allows admin users to edit theme and plugin files directly from the dashboardโ€”a major risk if an attacker gains access.

Add this to wp-config.php:

define('DISALLOW_FILE_EDIT', true);

๐Ÿ“Œ Removes โ€œTheme Editorโ€ and โ€œPlugin Editorโ€ from admin.


โœ… 2. Disable Plugin and Theme Installation & Updates#

To prevent installation or updating of themes and plugins from the dashboard:

define('DISALLOW_FILE_MODS', true);

โš ๏ธ Use this only if your site is stable and you handle updates manually via FTP or SSH.#

โœ… 3. Harden File and Directory Permissions (Linux)#

SSH into your server and run:

cd /var/www/html  # Replace with your path

# Set proper file permissions
find . -type f -exec chmod 644 {} \;

# Set proper directory permissions
find . -type d -exec chmod 755 {} \;

# Lock sensitive files
chmod 600 wp-config.php
chmod 644 .htaccess

โœ… 4. Lock Critical Files with chattr (Immutable Attribute)#

chattr prevents even root users or malware from modifying files:

sudo chattr +i wp-config.php
sudo chattr +i .htaccess

To unlock:

sudo chattr -i wp-config.php

๐Ÿ›ก๏ธ This defends against common malware that rewrites .htaccess or config files.


โœ… 5. Lock Core Directories (Advanced)#

If youโ€™re not making frequent changes:

sudo chattr -R +i wp-includes
sudo chattr -R +i wp-admin

โŒ Do not apply this to wp-content/uploads, or media uploads will break.


โœ… 6. Block PHP Execution in Uploads#

Prevent attackers from uploading backdoor scripts:

๐Ÿ“For Apache (/wp-content/uploads/.htaccess):

<FilesMatch "\.php$">
    Order Deny,Allow
    Deny from all
</FilesMatch>

๐Ÿ“For Nginx:

location ~* /wp-content/uploads/.*\.php$ {
    deny all;
}

โœ… 7. Secure .htaccess Rules#

Append this to your .htaccess file:

NOTE

Replace yourdomain.com with your actual domain


# WordPress Rules
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

# Enable Gzip Compression (Improves Speed)
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css application/javascript application/json
</IfModule>

# Enable Browser Caching (Improves Performance)
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/x-javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 2 days"
</IfModule>

# Block Hotlinking (Prevents Image Theft)
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
    RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
</IfModule>

# Enable Keep-Alive (Reduces Server Overhead)
<IfModule mod_headers.c>
    Header set Connection keep-alive
</IfModule>

# Disable Directory Browsing (Security)
Options -Indexes

# Protect wp-config.php (WordPress Security)
<Files wp-config.php>
    order allow,deny
    deny from all
</Files>

โœ… 8. Disable XML-RPC (If not used)#

xmlrpc.php is often abused by bots for brute-force and DDoS attacks.

๐Ÿ“Add to .htaccess:

<Files xmlrpc.php>
    order deny,allow
    deny from all
</Files>

โœ… 9. Use Strong Login Protections#

  • Limit login attempts (use plugin or Nginx/Fail2Ban)
  • Use 2FA (Google Authenticator or Authy)
  • Change /wp-login.php URL using plugins like WPS Hide Login
  • Rename the default admin username
  • Use long, random passwords for all accounts

โœ… 10. Regular Backups#

Use tools like:

  • UpdraftPlus
  • All-in-One WP Migration

โ˜๏ธ Ensure your backups are stored off-site (Google Drive, S3, etc.).


โœ… 11. Secure Database Access#

In wp-config.php, add:

define('DB_USER', 'yourdbuser');
define('DB_PASSWORD', 'strongpassword');
define('DB_HOST', 'localhost');

Also, rename your table prefix from wp_ to something custom like x9y_ to protect from SQL injection bots.


โœ… 12. Enable SSL (HTTPS)#

  • Use Letโ€™s Encrypt or a commercial SSL
  • Force HTTPS redirection in .htaccess or Nginx config
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

โœ… 13. Keep Everything Updated#

Outdated plugins and themes are a major attack vector.

  • Enable auto-updates for security releases
  • Remove unused plugins/themes

NOTE

Use a WAF or CDN with Security Services like: Cloudflare


๐Ÿ” Ultimate Guide to Securing Your WordPress Site
https://www.itsnooblk.com/posts/wordpress-security/
Author
Lahiru Sandaruwan Liyanage
Published at
2025-05-26
License
MIT License