Skip to main content

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

FlagDescription
NAMEConstant or variable name
VALUENew value to set
--type=TYPEconstant (default) or variable
--rawStore the value as a PHP expression (not a string literal)
--anchor=TEXTInsert new constant before/after this text in the file
--placement=SIDEbefore or after the anchor line
--addAdd 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

  1. Always use --raw for booleans and integers — avoids string vs boolean PHP confusion.
  2. Back up wp-config.php before automated changes — it's a critical file.
  3. Use WP_ENVIRONMENT_TYPE to set environment in one place; plugins read it via wp_get_environment_type().
  4. Verify immediately with wp config get NAME after setting.
  5. Use --anchor when adding new, unrelated constants so they're placed logically in the file.

Troubleshooting

ProblemCauseFix
Constant stored as string "true"--raw not usedRe-set with --raw flag
Error: Constant not foundDoesn't exist yetwp config set adds it by default — no error unless --add=false
wp-config.php corruptedRace condition or write errorRestore from backup
New constant in wrong placeDefault anchor is /* That's allUse --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