Skip to content
armanazij.com

WordPress 7.2's Secrets API: stop keeping API keys in wp_options

WordPress 7.2 adds an encrypted Secrets API for plugin credentials. What it does, what it deliberately does not do, how to migrate a plugin, and what to test before the December 8 release.

Rejaul Alom Khan (Arman Azij), Laravel and WordPress developer in Dhaka, Bangladesh Arman Azij 8 min read
WordPress 7.2's Secrets API: stop keeping API keys in wp_options, by Rejaul Alom Khan (Arman Azij)

Why wp_options was never the right place

Almost every WordPress plugin that talks to an outside service keeps a credential somewhere: a payment gateway key, an SMTP password, an OAuth client secret, an API token for an AI provider. For twenty years the answer to "where?" has been update_option(). The value lands in wp_options as plain text, and from that moment it travels with the site. Every database backup carries it. Every SQL export a developer downloads to debug a bug carries it. Every staging copy, every migration plugin archive, every "can you send me the DB?" message carries it.

That is the problem WordPress 7.2 sets out to fix with the Secrets API. Release candidate 1 shipped on November 17 and the final release is scheduled for December 8, so now is the time to look at it. I have props in every Core release from 6.6 to 7.1, and part of my Core work is testing patches and writing test reports, so this post is written from the testing side: what the API does, what it deliberately does not do, and what to try before December.

One honest caveat first. The API went through visible changes between Beta 1 and Beta 5, and the announcement post on Make/Core drew a serious objection in its comments about the design. Everything below describes the API as documented on Make/Core during Beta 5. Check the 7.2 Field Guide published with RC1 before you rely on a detail.

What the Secrets API does

The idea is small enough to fit in one sentence: credentials are stored encrypted, and a plugin only ever sees the decrypted value at the exact moment it uses it.

The public functions are:

  • wp_set_secret( 'my-plugin/api-key', $value ) stores a value. It returns a WP_Error if nothing was stored.
  • wp_get_secret( 'my-plugin/api-key' ) returns a WP_Secret object, null if the secret was never stored, or a WP_Error if it exists but cannot be read right now.
  • wp_delete_secret() removes a secret.
  • wp_list_secrets() returns names and metadata, never values.
  • wp_import_option_as_secret() copies an existing option into a secret and flags it for rotation, because the old value has already been in backups.
  • wp_retire_secret_version() deletes the previous version after a rotation.
  • wp_is_secrets_api_available() and wp_secrets_provider_is_writable() tell you whether you can read and write at all.

Names follow the pattern plugin-slug/secret-name, lowercase letters, digits, hyphens and underscores, with exactly one slash. A name without a slash still works but triggers _doing_it_wrong(). The prefix stops two plugins from overwriting each other. It is not access control: any PHP code running on the site can read any secret, and the announcement is explicit about that.

The WP_Secret object is the detail I like most. If you var_dump() it, log it, JSON-encode it or print it, you get [secret:my-plugin/api-key]. Serializing or cloning it throws. The only way to get the real value is reveal(), and the guidance is to call reveal() where the value is used and never cache the object. That one design choice closes the most common leak in plugin code: a debug log line that happened to include the whole settings array.

The key in wp-config.php

Secrets are encrypted with libsodium (XChaCha20-Poly1305) and stored in the options tables. The per-secret keys derive from a random root key, and that root key is itself encrypted with a new constant, WP_SECRETS_KEY, which lives in wp-config.php next to the salts.

This is the part that changed during the beta cycle. Early betas derived a fallback key from LOGGED_IN_KEY and LOGGED_IN_SALT when the constant was missing, which meant that rotating your salts silently made every secret unreadable. That fallback is gone. Without a usable WP_SECRETS_KEY, the API stores nothing, and every write and every read returns a secret_key_unavailable error.

New installs get the key from the installer alongside the salts. Existing sites need to add one. Site Health generates a ready-to-paste line, or you can make one yourself:

php -r 'echo base64_encode( random_bytes( 32 ) ), "\n";'
define( 'WP_SECRETS_KEY', 'paste-the-base64-value-here' );

An empty value, the placeholder from wp-config-sample.php, or a value copied from one of the authentication salts is rejected. And unlike the salts, you do not rotate this key casually. Changing or removing it makes every stored secret unreadable until the old value is restored.

Rotation and the previous version

Every secret keeps one previous version. Writing a new value moves the current one to WP_Secret_Version::PREVIOUS. The point is credential rotation without downtime: you issue a new API key at the provider, store it, and if a request with the new key fails during the rollout window you can fall back to the previous one. When you are confident, wp_retire_secret_version() deletes it.

A small but thoughtful detail: saving the same value again, which is exactly what a settings form does on every submit, does not overwrite the previous version. Secrets imported from options are flagged as pending rotation, and Site Health reminds you about them, because the old value has already lived in plain text somewhere.

Migrating an existing plugin

Here is the shape I would use in a plugin that must keep working on WordPress 7.1 and earlier. The API is optional, existing options keep working, and function_exists() is the supported way to detect it.

function myplugin_api_key(): ?string {
    if ( ! function_exists( 'wp_get_secret' ) || ! wp_is_secrets_api_available() ) {
        // Pre-7.2, or 7.2 without a usable WP_SECRETS_KEY yet.
        return get_option( 'myplugin_api_key' ) ?: null;
    }

    $secret = wp_get_secret( 'myplugin/api-key' );

    if ( is_wp_error( $secret ) ) {
        // The secret exists but cannot be read right now (key or database unavailable).
        // Do not treat this as "missing" and do not ask the user for a new one.
        return null;
    }

    return $secret?->reveal();
}

And on the settings screen:

if ( ! current_user_can( 'manage_options' ) ) {
    return;
}

if ( ! wp_secrets_provider_is_writable() ) {
    // Host-managed secrets: show a notice, never fall back to update_option().
    return;
}

$saved = wp_set_secret( 'myplugin/api-key', $submitted_key );

if ( is_wp_error( $saved ) ) {
    // Show the error; nothing was stored.
}

Two things in that code are easy to miss. First, the Secrets API functions do not check capabilities, because they also run from cron and REST, so the current_user_can() check is your job wherever a user enters or manages a value. Second, the distinction between null and WP_Error matters. null means the secret was never stored. WP_Error means it is there and you cannot read it at the moment. Prompting the user for a fresh key on a WP_Error would throw away a valid credential.

Host-managed providers and the secrets.php drop-in

Hosts can replace parts of the system through a wp-content/secrets.php drop-in that loads like db.php. It can swap three pieces: WP_Secrets_Keyring (keep the key that wraps the root key in a KMS or HSM), WP_Secrets_Store (keep the encrypted records outside the options tables), or WP_Secrets_Provider (hand the whole API to an external secret manager). The feature plugin that preceded the Core merge already ships AWS Secrets Manager and HashiCorp Vault providers.

On a read-only provider, wp_secrets_provider_is_writable() returns false and wp_set_secret() returns a WP_Error. Reads work the same everywhere. For plugin authors this means one thing: a settings screen should show a notice instead of an input field when writes are not possible.

If you are writing a drop-in, note the late-beta change: the keyring, store and provider are now abstract classes, not interfaces. A drop-in still using implements is a fatal error on every request, and the API cannot catch it. If the drop-in fails to load, every call returns an error and Site Health reports it. There is no silent fallback to defaults, by design.

What it does not do

  • There is no admin UI in 7.2. Nothing in wp-admin lists or edits secrets.
  • There are no WP-CLI commands yet. wp config create is being updated to add the key.
  • It is not access control. Any code on the site can reveal() any secret.
  • It does not protect against malicious code already running inside WordPress. It protects against leaked databases, backups and exports.
  • It does not check capabilities for you.

How to test it before December

  • Spin up a test site on RC1 with wp core update --version=7.2-RC1, the WordPress Beta Tester plugin, or a Playground instance.
  • Open Site Health. Under Security you should see three new tests: the key source, undecryptable secrets, and credentials pending rotation. The Info tab gains a Secrets API section with the provider, keyring, key source and secret count.
  • Store a secret, take a database export, and confirm the value is not in it.
  • If you stored secrets on an earlier beta without WP_SECRETS_KEY, follow the re-encryption steps in the Make/Core post before touching your salts. On multisite, every network's root key must be re-encrypted, not only the current one.
  • The conformance tests live under tests/phpunit/includes/secrets/ in wordpress-develop if you want to see the expected behaviour as code.

Takeaways

  • Credentials in wp_options leak through backups and exports. The Secrets API stores them encrypted and hands them out only through WP_Secret::reveal().
  • Add WP_SECRETS_KEY to wp-config.php on existing sites, and never rotate it like a salt.
  • Feature-detect with function_exists( 'wp_get_secret' ) and wp_is_secrets_api_available(), and handle null and WP_Error differently.
  • Check capabilities yourself, and show a notice instead of an input on read-only providers.
  • Test on RC1 now, and read the Field Guide before 7.2 ships on December 8.
Tags: #WordPress #Security #WordPress Core

More articles

All posts
Abilities and MCP: making your WordPress plugin callable by AI agents, by Rejaul Alom Khan (Arman Azij)

6 min read

Abilities and MCP: making your WordPress plugin callable by AI agents

The Abilities API has been in WordPress Core since 6.9, and the MCP Adapter is now a WordPress.org plugin. How to register typed, permission-checked abilities, expose them over REST and MCP, and keep AI clients safely limited.

  • #WordPress
  • #AI
  • #Security
Rejaul Alom Khan (Arman Azij), Laravel and WordPress developer in Dhaka, Bangladesh

Let’s talk

Arman Azij