Skip to main content

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 / FlagDescription
NAMEName of the constant or variable (e.g. WP_DEBUG)
VALUEValue to set
--addAdd the constant if it does not exist; do nothing if it does
--rawStore the value without quotes (needed for booleans, integers, null)
--type=constantTarget a PHP define() constant (default)
--type=variableTarget a PHP $variable
--anchor=ANCHORInsert after this anchor string in the file
--placement=before/afterWhere to place relative to anchor
--separator=SEPUse a custom separator when writing value
--config-file=PATHTarget 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.

CommandWritten to filePHP evaluation
wp config set WP_DEBUG truedefine( 'WP_DEBUG', 'true' );string "true" (truthy but wrong type)
wp config set WP_DEBUG true --rawdefine( 'WP_DEBUG', true );boolean true
wp config set WP_POST_REVISIONS 5 --rawdefine( '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 neededExample
WP_DEBUGwp config set WP_DEBUG true --raw
WP_DEBUG_LOGwp config set WP_DEBUG_LOG true --raw
WP_CACHEwp config set WP_CACHE true --raw
WP_POST_REVISIONSwp config set WP_POST_REVISIONS 5 --raw
WP_MEMORY_LIMITwp config set WP_MEMORY_LIMIT '256M'
WP_HOMEwp config set WP_HOME 'https://example.com'
FORCE_SSL_ADMINwp config set FORCE_SSL_ADMIN true --raw
DISALLOW_FILE_EDITwp config set DISALLOW_FILE_EDIT true --raw

Best Practices

  1. Always use --raw for booleans (true/false) and integers.
  2. Verify with wp config list after setting values.
  3. Use in deployment scripts to environment-configure WordPress without file edits.
  4. Prefer --add in idempotent scripts to avoid re-setting existing values.

Troubleshooting

ProblemCauseFix
Constant is set but behaves wrongMissing --raw for boolean/intRe-run with --raw
Error: The constant is not defined (on update)Key doesn't exist yetRun without --add to force create, or run wp config create first
Permission deniedCan't write to wp-config.phpFix 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