Custom wp-config.php | Environment-Aware WordPress Setup
This wp-config.php goes beyond the default — it automatically detects the environment (local, staging, production) and adjusts settings accordingly. This ensures consistent, secure, and predictable behavior across all stages of development and deployment.
Key Highlights
🌍 Automatic Environment Detection
get_environment()inspects the domain ($_SERVER['HTTP_HOST']) to decide the environment:- Local: matches
localhost,127.0.0.1,.local,.test,.dev, orloc. - Staging: matches domains containing
staging,bbcdev.be, or starting withdev. - Production: default if no other match
- Local: matches
- WP-CLI Override: when running via
wp-cli, you can passWP_ENVto manually set the environment.
⚙ Environment-Specific Settings
Each environment defines its debugging level, script handling, and database credentials:
Local
- Full debugging (
WP_DEBUG,WP_DEBUG_LOG,WP_DEBUG_DISPLAYalltrue) - Script and CSS compression disabled for easier troubleshooting
- Example DB credentials for local dev (
root/root)
Staging
- Debugging enabled but not displayed in-browser
- Script compression enabled for performance testing
- Example DB credentials for staging
Production
- All debugging disabled
- Script compression enabled
- Example DB credentials for live site
🔐 Security Keys
- Includes WordPress authentication salts & keys for secure sessions.
- Can be regenerated anytime using the official WordPress secret key generator.
📦 Database Table Prefix
- Set via
$table_prefixfor potential multi-install databases.
🛠 Additional Custom Constants
- CAPTCHA keys (
CAPTCHA_SITE_KEY/CAPTCHA_SECRET_KEY) for spam prevention FS_METHODset todirect— enables file changes without FTPDISALLOW_FILE_EDITset totrue— prevents file editing in the WP Admin for security
📂 Path & Bootstrapping
- Defines the absolute path to WordPress (
ABSPATH) - Loads
wp-settings.phpto initialize WordPress
Why This Setup Works Well
- No manual toggling between dev/staging/production settings — it just works based on domain
- Safer production defaults to avoid accidental debug exposure
- Keeps environment configs centralized in one file
- Easy to adapt for multiple developers and hosting environments