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 / Flag | Description |
|---|---|
NAME | Name of the constant or variable to retrieve |
--type=constant | Read a PHP define() constant (default) |
--type=variable | Read a PHP $variable |
--format=FORMAT | Output format: table, json, csv, yaml, var_export |
--config-file=PATH | Target 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
- Use in scripts to avoid hardcoding values that already live in
wp-config.php. - Redirect stderr with
2>/dev/nullwhen a key might not exist to avoid script errors. - Prefer
--format=jsonwhen passing values between tools or scripts. - Use
wp config listwhen you need to inspect all values at once.
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
Error: The constant is not defined | Key does not exist in config | Verify with wp config list |
| Empty output for variable | Wrong --type used | Add --type=variable for $table_prefix etc. |
| Permission denied | Can't read wp-config.php | Check 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
wp config list— see all constants at once.wp config set— update a value.wp config delete— remove a constant.