wp config set
Overview
Add or update any constant or variable in wp-config.php from the terminal. Use this to toggle debug mode, change database credentials, set environment flags, or add custom constants — all without manually editing PHP files.
What It Does
wp config set modifies wp-config.php in place. It uses static analysis to locate the correct define() call or $variable assignment and updates or inserts it. PHP execution is not required.
Syntax
wp config set <name> <value> [OPTIONS]
Arguments & Options
| Flag | Description |
|---|---|
NAME | Constant or variable name |
VALUE | New value to set |
--type=TYPE | constant (default) or variable |
--raw | Store the value as a PHP expression (not a string literal) |
--anchor=TEXT | Insert new constant before/after this text in the file |
--placement=SIDE | before or after the anchor line |
--add | Add the constant if it doesn't exist (default behaviour) |
Basic Usage
Enable WP_DEBUG
wp config set WP_DEBUG true --raw
Disable WP_DEBUG
wp config set WP_DEBUG false --raw
Set a string constant
wp config set WP_ENVIRONMENT_TYPE "staging"
Change table prefix
wp config set table_prefix "mysite_" --type=variable
Set memory limit
wp config set WP_MEMORY_LIMIT "256M"
Using --raw for PHP Boolean/Integer Values
Without --raw, values are stored as strings:
define('WP_DEBUG', 'true'); // BAD — stored as string
With --raw:
define('WP_DEBUG', true); // CORRECT — PHP boolean
Always use --raw for booleans and integers:
wp config set WP_DEBUG true --raw
wp config set WP_DEBUG_LOG true --raw
wp config set WP_POST_REVISIONS 5 --raw
Real-World Scenarios
Scenario 1: Enable debug mode for troubleshooting
wp config set WP_DEBUG true --raw
wp config set WP_DEBUG_LOG true --raw
wp config set WP_DEBUG_DISPLAY false --raw
echo "Debug logging enabled — check /wp-content/debug.log"
Scenario 2: Disable debug mode before deployment
#!/bin/bash
# pre-deploy-hardening.sh
wp config set WP_DEBUG false --raw
wp config set WP_DEBUG_LOG false --raw
wp config set WP_DEBUG_DISPLAY false --raw
wp config set DISALLOW_FILE_EDIT true --raw
wp config set FORCE_SSL_ADMIN true --raw
wp config set WP_AUTO_UPDATE_CORE '"minor"' --raw
echo "Security constants applied."
Scenario 3: Set environment type for plugin conditionals
# Development
wp config set WP_ENVIRONMENT_TYPE "development"
# Staging
wp config set WP_ENVIRONMENT_TYPE "staging"
# Production
wp config set WP_ENVIRONMENT_TYPE "production"
Scenario 4: Add a custom API key constant
wp config set MY_PLUGIN_API_KEY "sk-abc123xyz789" \
--anchor="/* That's all" \
--placement=before
Scenario 5: Update DB host after server migration
wp config set DB_HOST "new-db.internal.example.com"
wp db check && echo "DB connection OK" || echo "DB connection FAILED"
Safety: Backup Before Set
# Always back up wp-config.php before scripted changes
cp wp-config.php wp-config.php.bak-$(date +%Y%m%d)
wp config set WP_DEBUG true --raw
Best Practices
- Always use
--rawfor booleans and integers — avoids string vs boolean PHP confusion. - Back up
wp-config.phpbefore automated changes — it's a critical file. - Use
WP_ENVIRONMENT_TYPEto set environment in one place; plugins read it viawp_get_environment_type(). - Verify immediately with
wp config get NAMEafter setting. - Use
--anchorwhen adding new, unrelated constants so they're placed logically in the file.
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
Constant stored as string "true" | --raw not used | Re-set with --raw flag |
Error: Constant not found | Doesn't exist yet | wp config set adds it by default — no error unless --add=false |
wp-config.php corrupted | Race condition or write error | Restore from backup |
| New constant in wrong place | Default anchor is /* That's all | Use --anchor and --placement to control location |
Quick Reference
wp config set WP_DEBUG true --raw # PHP boolean
wp config set WP_MEMORY_LIMIT "256M" # String constant
wp config set table_prefix "mysite_" --type=variable
wp config set DISALLOW_FILE_EDIT true --raw
wp config get <name> # Verify after set
Next Steps
wp config get— read values from the config.wp config create— generate a newwp-config.php.wp core install— complete a WordPress installation after configuring.