Skip to main content

wp config get

Overview

Read a single constant or variable from wp-config.php without opening the file. Ideal for scripting, auditing, and conditional logic in deployment pipelines.

What It Does

wp config get reads and prints the current value of a named PHP constant or variable from wp-config.php. It returns just the value, making it easy to capture in shell scripts.

Syntax

wp config get <name> [--type=<type>] [--format=<format>] [--config-file=<path>]

Arguments & Options

Argument / FlagDescription
NAMEName of the constant or variable to retrieve
--type=constantRead a PHP define() constant (default)
--type=variableRead a PHP $variable
--format=FORMATOutput format: table, json, csv, yaml, var_export
--config-file=PATHTarget a config at a custom path

Basic Usage

Get a constant value

wp config get DB_NAME

Output:

wpdb

Get a variable

wp config get table_prefix --type=variable

Output:

wp_

Get with JSON format

wp config get WP_DEBUG --format=json

Output:

{"name":"WP_DEBUG","value":"true","type":"constant"}

Real-World Scenarios

Scenario 1: Read DB name in a backup script

DB_NAME=$(wp config get DB_NAME)
mysqldump -u root -p "$DB_NAME" > "${DB_NAME}_backup.sql"

Scenario 2: Check if debug mode is enabled

DEBUG=$(wp config get WP_DEBUG 2>/dev/null)
if [ "$DEBUG" = "true" ]; then
echo "WARNING: WP_DEBUG is enabled — do not run in production!"
fi

Scenario 3: Capture table prefix during migration

PREFIX=$(wp config get table_prefix --type=variable)
echo "Current table prefix: ${PREFIX}"

Scenario 4: Audit config before promoting to production

for key in DB_HOST DB_NAME WP_DEBUG WP_CACHE; do
echo "${key}: $(wp config get ${key} 2>/dev/null || echo 'NOT SET')"
done

Best Practices

  1. Use in scripts to avoid hardcoding values that already live in wp-config.php.
  2. Redirect stderr with 2&gt;/dev/null when a key might not exist to avoid script errors.
  3. Prefer --format=json when passing values between tools or scripts.
  4. Use wp config list when you need to inspect all values at once.

Troubleshooting

ProblemCauseFix
Error: The constant is not definedKey does not exist in configVerify with wp config list
Empty output for variableWrong --type usedAdd --type=variable for $table_prefix etc.
Permission deniedCan't read wp-config.phpCheck file permissions and user

Quick Reference

wp config get DB_NAME                          # Get database name
wp config get DB_HOST # Get database host
wp config get WP_DEBUG # Get debug flag
wp config get table_prefix --type=variable # Get table prefix
wp config get DB_NAME --format=json # JSON output

Next Steps