Secrets in WordPress: keep keys out of wp-config.php, git and backups
A typical wp-config.php holds the database password, the salts, an SMTP password, payment and API keys. The file gets copied to staging, ends up in backups that are stored for years, and sometimes in a git repository. Each copy is a place where the secrets can leak. Moving secrets into the environment of the PHP process, or into a secret store that injects them at deploy time, keeps wp-config.php free of them and makes rotation a configuration change instead of a file edit on every server.
Problem: An agency found a client’s Stripe live key in a staging database dump shared with a freelancer, and the same wp-config.php with production credentials is used as a template for new staging sites.
Solution: Read secrets in wp-config.php from environment variables set in the PHP-FPM pool or a root-only env file, use different credentials per environment, keep the actual values in a secret manager (a vault, the hosting panel, or the CI system’s secrets) that writes them at deploy time, store plugin API keys as constants instead of options in the database, and rotate anything that may have leaked.
Read from the environment
// wp-config.php
function ha_env( string $name, ?string $default = null ): ?string {
$value = getenv( $name );
return ( false === $value || '' === $value ) ? $default : $value;
}
define( 'DB_NAME', ha_env( 'WP_DB_NAME', 'wordpress' ) );
define( 'DB_USER', ha_env( 'WP_DB_USER' ) );
define( 'DB_PASSWORD', ha_env( 'WP_DB_PASSWORD' ) );
define( 'DB_HOST', ha_env( 'WP_DB_HOST', 'localhost' ) );
define( 'AUTH_KEY', ha_env( 'WP_AUTH_KEY' ) );
// ... the other keys and salts the same way
define( 'HA_STRIPE_SECRET', ha_env( 'HA_STRIPE_SECRET' ) );
; /etc/php/8.3/fpm/pool.d/example.conf (readable by root only) env[WP_DB_PASSWORD] = "…" env[WP_AUTH_KEY] = "…" env[HA_STRIPE_SECRET] = "sk_live_…"
PHP-FPM passes env[] values to the PHP processes of that pool; with clear_env = yes (the default), only these are visible. The pool file is owned by root and not readable by the web server user, unlike wp-config.php, which PHP must be able to read. For WP-CLI and cron, which do not run through FPM, put the same variables into an env file loaded by the shell or the systemd unit of the job.
Constants instead of options for plugin keys
Many plugins store API keys in wp_options, which means they are in every database dump and visible to every administrator. If a plugin supports it, define the key as a constant; for your own plugins, support both:
function ha_stripe_secret(): string {
return defined( 'HA_STRIPE_SECRET' ) && HA_STRIPE_SECRET ? HA_STRIPE_SECRET : (string) get_option( 'ha_stripe_secret', '' );
}
The settings page then shows “defined in configuration” instead of the field, and staging gets a test key through its own environment.
Where the values come from
- Hosting panel or platform: many managed hosts set environment variables per site.
- CI secrets: GitHub Actions or GitLab CI write the pool file on deploy from their encrypted secrets.
- A vault: HashiCorp Vault, Infisical, Doppler or a cloud secret manager, read by a deploy step or an agent that renders the pool file.
The common rule: the value exists in one place, and servers get it at deploy time. wp-config.php itself can then be versioned in git, because it contains no secrets.
After a leak
Change the leaked credentials at their source (database user password, Stripe key, SMTP password), then update the secret store and redeploy. New salts in wp-config.php log out all users, which is the right reaction if session cookies may have leaked too. Protecting the file itself on the web server remains good practice: protect wp-config.php.
NOTE: Environment variables are visible to everything running as the same user and in phpinfo() output; never leave a phpinfo() page on a server. Separate environments with separate credentials, as set up in a debugging setup per environment, limit the damage of a leak from staging.