Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 6 min read

Hardening a WordPress site: a practical checklist for developers

If you maintain WordPress sites, most of the risk is not a mysterious flaw in core. It is an outdated plugin, a writable directory, or an administrator who can edit PHP from the dashboard. Hardening shrinks that attack s

If you maintain WordPress sites, most of the risk is not a mysterious flaw in core. It is an outdated plugin, a writable directory, or an administrator who can edit PHP from the dashboard. Hardening shrinks that attack surface and contains the damage if something still gets through.

The WordPress project treats this as risk reduction, not risk elimination, and keeps a detailed Hardening WordPress guide up to date. What follows is the subset I apply before a site goes to production, plus a few server-level checks the dashboard will not show you.

1. Treat wp-config.php as a secret, not a settings file

wp-config.php holds the database password and the authentication keys. The official guide allows you to store it one directory level above the WordPress root, and it recommends permissions of 400 or 440 so that only you and the web server can read it. On Apache, you can also deny HTTP access at the very top of .htaccess:

<Files "wp-config.php">
Require all denied
</Files>

Moving the file has trade-offs if you do it carelessly, so test the install afterwards. The change that matters more on almost every site is this constant:

define( 'DISALLOW_FILE_EDIT', true );

That removes the theme and plugin editors from the dashboard Ò€” the equivalent of taking away the edit_themes, edit_plugins, and edit_files capabilities. It will not stop an attacker who can already upload files, but it closes a common next step after a stolen admin session. Leave debugging off in production.

2. Use a tight permission scheme, then loosen it only when you must

Writable files are convenient and, on shared hosting, also a hazard. The file permissions section of the official guide is the scheme I start from:

  • The WordPress root, wp-admin, and wp-includes should be writable only by your user account, except .htaccess if you want WordPress to write rewrite rules.
  • wp-content is the exception: uploads need to be writable by the web server process.
  • Plugin files under wp-content/plugins should be writable only by your user account. Theme files can follow the same rule if you are not using the built-in theme editor.

If you have shell access, the documented baseline is directories at 755 and files at 644:

find /path/to/your/wordpress/install/ -type d -exec chmod 755 {} \;
find /path/to/your/wordpress/install/ -type f -exec chmod 644 {} \;

Then tighten wp-config.php to 400 or 440. When WordPress runs an automatic update, it performs file operations as the file owner and resets files to 0644 and directories to 0755, so re-check permissions after a core update if you use a stricter scheme.

3. Give the database user only what the app needs

If you host more than one WordPress site on a server, keep them in separate databases with separate users, so a compromise of one install is harder to turn into a compromise of the others.

For day-to-day publishing, media, comments, users, and plugin installs, the official guide says the MySQL user only needs SELECT, INSERT, UPDATE, and DELETE. Privileges such as DROP, ALTER, and GRANT can be revoked to limit damage. Read the warning that follows: some plugin installs and major upgrades change the schema. The projectÒ€ℒs own guidance is that revoking structural privileges is not the default recommendation. If you still want that tighter posture, grant the extra privileges back before an update, keep backups you have actually restored, and tighten the grants again only after the update succeeds.

4. Shrink the admin surface

Two kinds of attacks show up again and again: crafted requests against outdated plugins, and brute-force guessing against wp-login.php. The brute force documentation is worth a single careful read.

  • Require HTTPS for the admin area and the login screen so credentials and cookies are not sent in the clear. The hardening guide points to administration over SSL for this second layer.
  • Use unique, long passwords and two-factor authentication on every administrator account. Do not share one administrator login across a team, and give each person the least capability they need.
  • A server-level password in front of /wp-admin/ will stop a lot of automated probes, but it can break wp-admin/admin-ajax.php if you wrap the whole directory blindly. Allow that endpoint through, or protect only wp-login.php, and test the block editor after you add the rule.

If the site does not use the WordPress mobile app, Jetpack, or pingbacks, you can also disable XML-RPC at the server Ò€” only after you have confirmed nothing in the stack still depends on xmlrpc.php.

5. Block direct access to include-only PHP

PHP files under wp-includes are not meant to be hit in the browser. On Apache, the official guide adds a second layer with mod_rewrite. Place this outside the # BEGIN WordPress and # END WordPress markers, or a permalink flush will overwrite it:

# Block the include-only files.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^wp-admin/includes/ - [F,L]
RewriteRule !^wp-includes/ - [S=3]
RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
RewriteRule ^wp-includes/theme-compat/ - [F,L]
</IfModule>

Skip or adapt the wp-includes/[^/]+\.php rule on Multisite; it can break ms-files.php. On Nginx, the same idea is a location block that denies those PHP files rather than a rewrite copied from Apache.

6. Treat plugins as code you agreed to run

Keep plugins updated, and delete the ones you are not using. A deactivated plugin is still files on the disk. Install only from a source you can name Ò€” start with the WordPress.org plugin directory Ò€” and check the last updated date before you add anything.

A firewall plugin or a web application firewall can help. If you add one, watch what it injects on the front end. Extra JavaScript on every page can delay Interaction to Next Paint, one of the Core Web Vitals Google uses when it assesses experience.

Security headers belong at the server or CDN. A cautious starting set is X-Content-Type-Options: nosniff, a strict Referrer-Policy, and Content-Security-Policy with frame-ancestors. MDNÒ€ℒs Content-Security-Policy documentation is the right reference. Introduce CSP in report-only mode first: a strict policy will otherwise break the block editor and a surprising number of plugins. This is the same class of work as the OWASP Top Ten injection and misconfiguration items Ò€” limit what the browser will execute, and do not ship a policy you have not tested.

7. Keep a backup you have restored

A backup that has never been restored is a hope. Back up regularly, including the MySQL database, and keep a copy off the same server. After a hardening change or a core update, restore onto staging and confirm that the homepage, a form, and the login still work.

For a less technical walkthrough of updates, backups, two-factor login, and what to do if a site is already compromised, we published a step-by-step guide on how to secure a WordPress website. The developer work in this article sits underneath that checklist; it does not replace it.

A pre-deploy checklist

Use this as a deploy gate:

  • [ ] Core, themes, and plugins are current; unused plugins and themes are deleted (keep one default theme).
  • [ ] DISALLOW_FILE_EDIT is true; wp-config.php is not HTTP-readable and is mode 400 or 440.
  • [ ] Directories are 755, files are 644, and wp-content write access is limited to what uploads require.
  • [ ] The database user is unique to this site; structural privileges are understood and documented if you tightened them.
  • [ ] Admin and login run over HTTPS; administrators use unique credentials and two-factor authentication.
  • [ ] Include-only PHP under wp-includes and wp-admin/includes is blocked, and Multisite was considered.
  • [ ] Security headers are set at the server; CSP was tested against wp-admin and the front end.
  • [ ] A full backup was restored on staging after the last change.

None of this makes a site untouchable. It does make the common paths harder, and it gives you a way back when something still goes wrong.

Lokendra Singh, Founder at Aukark. Aukark is a Gurugram studio working on web, UI/UX, and SEO.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.