wp config set
Overview
Add a new constant or update an existing one in wp-config.php from the terminal. Eliminates manual file editing and makes environment-based config changes repeatable and scriptable.
What It Does
wp config set writes or updates a PHP constant (via define()) or variable (e.g. $table_prefix) in wp-config.php. By default it targets constants; use --type=variable for variables.
Syntax
wp config set <name> <value> [--add] [--raw] [--type=<type>] [--config-file=<path>]
Arguments & Options
| Argument / Flag | Description |
|---|---|
NAME | Name of the constant or variable (e.g. WP_DEBUG) |
VALUE | Value to set |
--add | Add the constant if it does not exist; do nothing if it does |
--raw | Store the value without quotes (needed for booleans, integers, null) |
--type=constant | Target a PHP define() constant (default) |
--type=variable | Target a PHP $variable |
--anchor=ANCHOR | Insert after this anchor string in the file |
--placement=before/after | Where to place relative to anchor |
--separator=SEP | Use a custom separator when writing value |
--config-file=PATH | Target a config at a custom path |
Basic Usage
Enable WordPress debug mode
wp config set WP_DEBUG true --raw
Set a string constant
wp config set WP_HOME 'https://example.com'
Set an integer constant
wp config set WP_POST_REVISIONS 5 --raw
Set a boolean to false
wp config set WP_DEBUG false --raw
Set a variable (e.g. table prefix)
wp config set table_prefix 'prod_' --type=variable
Add a constant only if it doesn't already exist
wp config set WP_CACHE true --raw --add
Expected Output
Success: Updated the constant 'WP_DEBUG' in the 'wp-config.php' file.
If adding a new constant:
Success: Added the constant 'WP_CACHE' to the 'wp-config.php' file.
The --raw Flag Explained
Without --raw, values are stored with quotes: define( 'WP_DEBUG', 'true' ); — which PHP treats as the string "true", not boolean true.
| Command | Written to file | PHP evaluation |
|---|---|---|
wp config set WP_DEBUG true | define( 'WP_DEBUG', 'true' ); | string "true" (truthy but wrong type) |
wp config set WP_DEBUG true --raw | define( 'WP_DEBUG', true ); | boolean true ✅ |
wp config set WP_POST_REVISIONS 5 --raw | define( 'WP_POST_REVISIONS', 5 ); | integer 5 ✅ |
Always use --raw for booleans and integers
Failing to use --raw is one of the most common WP-CLI config mistakes. The value will appear to be set but PHP will treat it as a string.
Real-World Scenarios
Scenario 1: Enable debug for troubleshooting, then disable
# Enable
wp config set WP_DEBUG true --raw
wp config set WP_DEBUG_LOG true --raw
wp config set WP_DEBUG_DISPLAY false --raw
# After debugging — disable
wp config set WP_DEBUG false --raw
wp config delete WP_DEBUG_LOG
wp config delete WP_DEBUG_DISPLAY
Scenario 2: Harden production configuration
wp config set DISALLOW_FILE_EDIT true --raw
wp config set DISALLOW_FILE_MODS true --raw
wp config set FORCE_SSL_ADMIN true --raw
wp config set WP_POST_REVISIONS 5 --raw
Scenario 3: Set memory limit and caching
wp config set WP_MEMORY_LIMIT '256M'
wp config set WP_MAX_MEMORY_LIMIT '512M'
wp config set WP_CACHE true --raw
Scenario 4: Configure site URLs programmatically
SITE_URL="https://newdomain.com"
wp config set WP_HOME "${SITE_URL}"
wp config set WP_SITEURL "${SITE_URL}"
Common Constants to Manage
| Constant | --raw needed | Example |
|---|---|---|
WP_DEBUG | ✅ | wp config set WP_DEBUG true --raw |
WP_DEBUG_LOG | ✅ | wp config set WP_DEBUG_LOG true --raw |
WP_CACHE | ✅ | wp config set WP_CACHE true --raw |
WP_POST_REVISIONS | ✅ | wp config set WP_POST_REVISIONS 5 --raw |
WP_MEMORY_LIMIT | ❌ | wp config set WP_MEMORY_LIMIT '256M' |
WP_HOME | ❌ | wp config set WP_HOME 'https://example.com' |
FORCE_SSL_ADMIN | ✅ | wp config set FORCE_SSL_ADMIN true --raw |
DISALLOW_FILE_EDIT | ✅ | wp config set DISALLOW_FILE_EDIT true --raw |
Best Practices
- Always use
--rawfor booleans (true/false) and integers. - Verify with
wp config listafter setting values. - Use in deployment scripts to environment-configure WordPress without file edits.
- Prefer
--addin idempotent scripts to avoid re-setting existing values.
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
| Constant is set but behaves wrong | Missing --raw for boolean/int | Re-run with --raw |
Error: The constant is not defined (on update) | Key doesn't exist yet | Run without --add to force create, or run wp config create first |
| Permission denied | Can't write to wp-config.php | Fix file ownership |
Quick Reference
wp config set WP_DEBUG true --raw # Boolean constant
wp config set WP_MEMORY_LIMIT '256M' # String constant
wp config set WP_POST_REVISIONS 5 --raw # Integer constant
wp config set table_prefix 'bp_' --type=variable # Variable
wp config set WP_CACHE true --raw --add # Only add if not exists
Next Steps
wp config get— confirm values.wp config delete— remove a constant.wp config list— audit the full config.wp config shuffle-salts— rotate authentication salts.