Most WordPress hacks don’t start with something clever. They start with a stolen admin password, an error message that gives away a file path, or a plugin left two years out of date. wp-config.php can’t fix weak passwords, but it can make each of those mistakes much less useful to an attacker.
This is part 1 of our Hardening WordPress series. Every setting below is a plain PHP constant: no plugin needed.
Before you start
- Access to your site’s filesSFTP, SSH or your hosting file manager
- A fresh backupIncluding a copy of wp-config.php itself
- HTTPS working on the siteNeeded for setting 2
Add everything above the line /* That's all, stop editing! Happy publishing. */. Anything below it runs after WordPress has started, and most of these settings will then do nothing.
The seven settings
Turn off the theme and plugin file editor
WordPress lets administrators edit theme and plugin PHP files from Appearance → Theme File Editor and Plugins → Plugin File Editor. If anyone ever gets into an admin account, that is a ready-made way to add malicious code. Turn it off:
define( 'DISALLOW_FILE_EDIT', true );Tested on WordPress 7.1 · PHP 8.3Both editor screens disappear. Updates and plugin installs still work. Our snippet library has more on this setting.
Deploy with Git or Composer?Go one step further with
define( 'DISALLOW_FILE_MODS', true );. It also blocks installing, updating and deleting plugins and themes from wp-admin, and stops automatic updates. Only use it when updates reach the site some other way.Always use HTTPS for logins and wp-admin
define( 'FORCE_SSL_ADMIN', true );Tested on WordPress 7.1 · PHP 8.3Logins and the admin area are then only ever served over HTTPS, so passwords and login cookies are never sent in plain text. If your site already redirects everything to HTTPS, this is a safety net for the day that redirect is misconfigured.
Never show PHP errors to visitors
Error messages can reveal file paths, plugin names and database details. On a live site they belong in a log, not on the page:
define( 'WP_DEBUG', false ); define( 'WP_DEBUG_DISPLAY', false ); @ini_set( 'display_errors', '0' );Tested on WordPress 7.1 · PHP 8.3WP_DEBUG_DISPLAYand theini_setline also cover errors that happen before WordPress has fully loaded.Log errors privately when you need them
When something breaks, you still want to see what happened. Turn debugging on with a log file outside the public web folder, so nobody can open it in a browser:
define( 'WP_DEBUG', true ); define( 'WP_DEBUG_DISPLAY', false ); define( 'WP_DEBUG_LOG', '/home/youraccount/logs/wp-debug.log' );Tested on WordPress 7.1 · PHP 8.3Use this instead of the lines in step 3, not as well. Replace the path with a folder your host gives you above
public_html. The default log,wp-content/debug.log, sits in a public folder and can often be downloaded by anyone who guesses the address.NoteSwitch back to step 3 when you are done. A debug log that grows for months can fill your disk.
Keep WordPress security releases automatic
WordPress installs minor releases (7.1.1, 7.1.2…) on its own by default. These are the security fixes. Make sure nothing on the site has turned that off, and say so in your config:
define( 'WP_AUTO_UPDATE_CORE', 'minor' );Tested on WordPress 7.1 · PHP 8.3Use
trueinstead if you also want major versions installed automatically. Search yourwp-config.phpand must-use plugins forAUTOMATIC_UPDATER_DISABLEDtoo: if it is set totrue, no automatic updates run at all.Stop scripts being saved in post content
By default, administrators and editors can save any HTML in posts, including
<script>tags. If one of those accounts is taken over, that is an easy way to put malware in front of every visitor:define( 'DISALLOW_UNFILTERED_HTML', true );Tested on WordPress 7.1 · PHP 8.3WordPress then filters unsafe tags out of everyone’s content, administrators included.
Check your embeds firstSome embed codes, tracking snippets and forms are pasted into posts as raw HTML with scripts. After this change WordPress strips those scripts when the post is saved. Move them into your theme, a plugin or a block that loads them properly before turning this on.
Refresh your secret keys and salts
The eight
AUTH_KEY,SECURE_AUTH_KEY… lines sign every login cookie. If they are still the placeholder text, were copied from another site, or might have leaked, replace them. With WP-CLI:wp config shuffle-salts Success: Shuffled the salt keys.No WP-CLI? Open the WordPress.org salt generator, copy all eight lines and paste them over the existing ones.
NoteEveryone is logged out when the salts change, you included. That is the point: any stolen login cookie stops working too.
Appearance and Plugins no longer show a file editor, /wp-admin/ always opens on https://, and a broken plugin shows a plain error page instead of a PHP message with file paths.
The site broke after editing wp-config.php3 common fixes
- White screen or “There has been a critical error”. Usually a missing semicolon or quote. Restore your backup copy of
wp-config.phpand add the lines again one at a time. - Redirect loop on /wp-admin/ after FORCE_SSL_ADMIN. Your site sits behind a proxy or CDN that ends HTTPS before WordPress. Ask your host how they pass the HTTPS flag, or remove the line until they do.
- An embed or form disappeared from a post. That is
DISALLOW_UNFILTERED_HTMLat work. See the warning in step 6.
Two more things worth doing
Move wp-config.php up one folder. WordPress looks for it one level above the WordPress folder if it isn’t next to wp-load.php. Above public_html, it can’t be served even if PHP ever stops working on the server. Not every host allows this, so check first.
Tighten its file permissions. wp-config.php only needs to be readable by the web server. 440 or 400 is a good target. Your host can tell you which works on their setup.
What doesn’t help much
Changing the database table prefix from wp_ is often listed as a security step. It doesn’t stop an attack that can already run SQL, and changing it on a live site is easy to get wrong. Spend the time on updates and strong logins instead.
The complete block
Here are the always-on settings together, ready to paste above the “stop editing” line:
// Security settings.
define( 'DISALLOW_FILE_EDIT', true );
define( 'FORCE_SSL_ADMIN', true );
define( 'WP_DEBUG', false );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', '0' );
define( 'WP_AUTO_UPDATE_CORE', 'minor' );
define( 'DISALLOW_UNFILTERED_HTML', true );Then refresh your salts. Next in this series: security headers, so the browser helps protect your visitors too.
Was this helpful?
Sorry about that. What was missing?
Thanks for your feedback!

