wp configphp
wp-config.php Guide: Secure, Optimize & Troubleshoot Your WordPress Site
The wp-config.php file is the single most important configuration file in your WordPress installation. It controls database connections, authentication keys, debugging modes, and security hardening — yet most site owners never touch it. This guide solves the most common wp-config.php problems and shows you exactly which settings matter.
Key Takeaways
- Location: wp-config.php lives in your WordPress root directory, one level above the
wp-adminfolder. - Never commit to version control: It contains secrets. Add it to
.gitignore. - Security keys are non-negotiable: Regenerate them if your site is ever compromised.
- Debug mode should always be off on production: But turn it on when troubleshooting — and never leave it on.
- A single typo can take your site down: Always back up wp-config.php before editing.
What Is wp-config.php?
Where does it live?
wp-config.php is a plain-text PHP configuration file located in the root directory of your WordPress installation (the same folder that contains wp-login.php and wp-content/). It is not the same as wp-config-sample.php, which is a template included with every WordPress download.
What does it control?
This one file orchestrates how WordPress connects to your database, how it authenticates users, where it stores temporary data, and how it behaves in development versus production. Every major setting below flows through this file:
| Constant / Setting | Purpose | Default Value |
|---|---|---|
DB_NAME | MySQL database name for WordPress | Set during installation |
DB_USER | Database username | Set during installation |
DB_PASSWORD | Database password | Set during installation |
DB_HOST | Database server hostname | localhost |
DB_CHARSET | Database character set | utf8mb4 |
DB_COLLATE | Database collation type | '' (auto) |
AUTH_KEY through NONCE_SALT | Encryption salts for cookies and sessions | Unique random strings |
WP_DEBUG | Enables or disables WordPress debug mode | false |
WP_MEMORY_LIMIT | PHP memory limit for WordPress | 40M (32-bit) / 64M (64-bit) |
WP_SITEURL | Overrides the WordPress URL | Database value |
WP_HOME | Overrides the site URL | Database value |
Common wp-config.php Problems & How to Fix Them
Problem 1: "Error Establishing a Database Connection"
This is the #1 reason sites go offline. It almost always means your database credentials in wp-config.php are wrong, or the database server is unreachable.
Checklist — troubleshoot in order:
- Verify
DB_NAME,DB_USER, andDB_PASSWORDmatch your hosting control panel (cPanel, Plesk, etc.). - Confirm
DB_HOSTis correct — some hosts use an IP address or a subdomain likemysql.example.cominstead oflocalhost. - Test the database credentials directly via phpMyAdmin or the MySQL CLI.
- Check that the database user has privileges on the specified database.
- Ensure the MySQL service is running on the server.
Problem 2: White Screen of Death After Editing wp-config.php
A missing semicolon, an extra space, or a typo in any line will produce a blank white page. PHP parses the file from top to bottom — the first syntax error stops everything.
Fix: Restore from your backup, then re-add changes one line at a time. The most common mistakes are:
- Forgetting the trailing semicolon (
;) at the end of a line. - Adding a comma after the last constant before
require_once. - Typo in a constant name (e.g.,
DB_HSTinstead ofDB_HOST). - Extra blank lines or whitespace between closing
?>and the next line.
Problem 3: Stuck in a Redirect Loop
If you recently changed WP_SITEURL or WP_HOME, your site may enter an infinite redirect loop between http and https, or between www and non-www.
Fix: Remove or comment out both WP_SITEURL and WP_HOME lines from wp-config.php, then update the URLs properly through the WordPress admin or your hosting panel.
Problem 4: Memory Exhaustion Errors
Errors like "Allowed memory size of 33554432 bytes exhausted" mean WordPress needs more PHP memory than currently allocated.
Fix — add this line above That's all, stop editing!:
define('WP_MEMORY_LIMIT', '256M');
If your host enforces a hard PHP memory cap, contact them — wp-config.php cannot override server-level limits.
Problem 5: Login Cookies Not Working (Brute-Force Vulnerability)
If your authentication salts are default, missing, or have been exposed, attackers can forge login cookies. This is a critical security risk.
Fix: Regenerate all authentication unique keys and salts at the WordPress.org secret-key service, then replace the existing AUTH_KEY through NONCE_SALT block in wp-config.php. Log out of all sessions immediately after.
wp-config.php Security Hardening Checklist
Apply these settings to significantly reduce your attack surface. Each one addresses a real, documented vulnerability vector.
| Setting | Code | What It Blocks | Priority |
|---|---|---|---|
| Disable file editor | define('DISALLOW_FILE_EDIT', true); | Admins cannot edit theme/plugin files via Dashboard | High |
| Disable plugin/theme installs | define(' |