ASHISH KANAJARIYA0%Preparing your experience
Skip to content

WordPress Secrets API: How WordPress 7.2 Stores API Keys Securely (Complete Guide)

Golden padlock on a laptop keyboard, showing encrypted credential storage with the WordPress Secrets API

Almost every modern WordPress site depends on at least one external service. Your SMTP plugin talks to an email provider, your forms plugin connects to a CRM, your store connects to a payment gateway, and since WordPress 7.0 your site may also hold credentials for one or more AI providers. Each of those connections needs an API key, a token or a client secret, and until now WordPress has never had a dedicated, built-in place to keep them.

The WordPress Secrets API, scheduled for WordPress 7.2, changes that. It gives plugins and Core a standard way to store credentials encrypted in the database, with the key that unlocks them kept in wp-config.php. In this guide you will learn what the Secrets API is, how it works under the hood, what site owners need to do before updating, and how plugin developers can start using wp_set_secret() and wp_get_secret() today.

What Is the WordPress Secrets API?

The Secrets API is a small set of functions in WordPress Core for storing sensitive values such as API keys, access tokens and OAuth client secrets. Instead of saving a key as a plain-text option in the wp_options table, a plugin hands the value to WordPress, and WordPress encrypts it before it ever touches the database.

The feature was proposed by Eric Mann on the Make WordPress Core blog in August 2026 and is tracked in Trac ticket #66187. The full technical details are published in the official dev note, The Secrets API in WordPress 7.2. If you want the bigger picture of everything else arriving in this release, read our overview of WordPress 7.2 new features.

Three design ideas shape the API:

  • Encryption is always on. There is no plain-text mode. Values are encrypted with libsodium using XChaCha20-Poly1305, a modern authenticated cipher.
  • The key lives outside the database. A random root key is stored encrypted with a new constant, WP_SECRETS_KEY, which sits in wp-config.php.
  • Values are hard to leak by accident. Reading a secret returns a WP_Secret object, not a string, and that object masks itself when printed or logged.

Why WordPress Needed a Built-in Way to Store Secrets

For most of WordPress history, the common pattern for a plugin settings screen was simple: the administrator pastes an API key into a field, and the plugin saves it with update_option(). That works well, and it is the reason connecting services to WordPress has always been so approachable. The trade-off is that the key ends up in the database exactly as it was typed.

Databases travel more than we often realise. They are copied into backups, exported to staging sites, shared with developers for debugging and imported into local environments. Every copy carries every stored key with it. The Secrets API is designed to reduce that exposure: an encrypted value in a database dump is not useful without the key from wp-config.php.

The timing also makes sense. WordPress 7.0 introduced the AI Client and Connectors, which encourage site owners to save AI provider credentials inside WordPress. We covered that in our guide to the WordPress AI Client and Connectors. As more credentials move into WordPress, a shared, well-reviewed storage layer benefits everyone, and the ticket notes that Connectors can move onto the Secrets API once it lands.

Digital lock illustration representing libsodium encryption of WordPress secrets

How the Secrets API Works Behind the Scenes

You do not need to understand cryptography to use the API, but knowing the moving parts helps you set it up correctly and explain it to clients.

Envelope encryption with a root key

WordPress generates a random root key for the site. That root key is itself stored encrypted using WP_SECRETS_KEY. Individual secrets are then encrypted with keys derived from the root key. This layered approach, often called envelope encryption, means the master key in wp-config.php never has to encrypt every individual record directly, and it makes future key changes more manageable.

Storage in the options table

By default, encrypted records are stored in the options tables, so there is no new database table to manage and existing backup tools keep working. Hosts can replace this storage layer, which we cover in the hosting section below.

Two versions for safe rotation

Each secret keeps one previous version. When you save a new value, the current value moves into the previous slot. That lets a plugin fall back to the old credential while a new one rolls out across services. Once you are confident the new credential works, wp_retire_secret_version() removes the previous version.

The Core Functions You Need to Know

The API surface is deliberately small. These are the functions described in the proposal and dev note:

  • wp_set_secret( $name, $value ) stores a value encrypted. It returns true, or a WP_Error if nothing could be stored.
  • wp_get_secret( $name ) 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( $name ) removes a stored credential.
  • wp_list_secrets() returns names and metadata such as fingerprints, never the values.
  • wp_retire_secret_version() deletes the previous version after a rotation.
  • wp_import_option_as_secret() copies an existing option into a secret, which is ideal for migrating older plugins.
  • wp_is_secrets_api_available() and wp_secrets_provider_is_writable() let you check whether the API can be used on the current site.

On Multisite, matching *_network_secret() functions such as wp_set_network_secret() store credentials for the whole network. Site-level and network-level functions never fall back to each other, which keeps the two scopes clearly separated.

The WP_Secret object

The most thoughtful part of the design is the WP_Secret value object. Its reveal() method is the only way to get the plain value. If you print, log, JSON-encode or var_dump() the object, you see a masked placeholder such as [secret:my-plugin/api-key]. Serializing or cloning the object throws an exception. In practice this means an API key is far less likely to end up in a debug log or an error report by accident. The guidance is to call reveal() exactly where the value is used and not to cache the object.

Naming rules

Secret names follow a plugin-slug/secret-name format using lowercase letters, digits, hyphens and underscores, with exactly one slash. A name without a slash still works but triggers a _doing_it_wrong() notice. The prefix prevents two plugins from accidentally overwriting each other. It is a naming convention, not a permission system.

Setting WP_SECRETS_KEY: The One Step Site Owners Must Take

The most important practical detail is that the Secrets API requires a WP_SECRETS_KEY constant in wp-config.php. During the beta period, an earlier version could fall back to a key derived from the salts, but the final design makes the constant required. Without a usable key, every write fails and every read returns a secret_key_unavailable error, so plugins that rely on the API will ask you to add it.

Here is what that means for different sites:

  • New installs get the key automatically from the installer.
  • Existing sites that update to 7.2 can copy a ready-made line from Tools → Site Health and paste it into wp-config.php, below the authentication keys and salts.
  • WP-CLI users will find that wp config create writes the key for WordPress 7.2 and later, and wp config shuffle-salts deliberately leaves it alone.

If you prefer to generate the key yourself, the dev note suggests this one-line PHP command, which prints a base64-encoded 32-byte random value:

php -r 'echo base64_encode( random_bytes( 32 ) ), "\n";'

Then add it to wp-config.php:

define( 'WP_SECRETS_KEY', 'paste-your-generated-value-here' );

A few rules keep this safe. WordPress will not accept an empty value, the sample placeholder or a value copied from one of your salts. Most importantly, treat this key like a password and never change it casually. Changing or removing it makes stored secrets unreadable until the original value is restored. Store a copy in your password manager alongside your hosting credentials, and make sure any server migration carries wp-config.php across unchanged.

How to Use the Secrets API in Your Plugin

Here is a straightforward pattern for a settings screen that saves an API key and a function that uses it later. Notice the capability check: the Secrets API functions do not check user permissions themselves, because they also need to work in cron jobs and REST requests. That responsibility stays with your code.

// Saving the key from a settings form.
if ( current_user_can( 'manage_options' ) && isset( $_POST['my_plugin_api_key'] ) ) {
    check_admin_referer( 'my_plugin_settings' );
    $result = wp_set_secret(
        'my-plugin/api-key',
        sanitize_text_field( wp_unslash( $_POST['my_plugin_api_key'] ) )
    );
    if ( is_wp_error( $result ) ) {
        // Show an admin notice, e.g. ask the owner to add WP_SECRETS_KEY.
    }
}

// Using the key when calling the service.
$secret = wp_get_secret( 'my-plugin/api-key' );

if ( $secret instanceof WP_Secret ) {
    $response = wp_remote_post( 'https://api.example.com/v1/send', array(
        'headers' => array( 'Authorization' => 'Bearer ' . $secret->reveal() ),
    ) );
} elseif ( is_wp_error( $secret ) ) {
    // The secret exists but cannot be read, e.g. the key changed.
} else {
    // null: no key saved yet. Prompt the admin to connect.
}

Supporting older WordPress versions

Your users will not all update on release day, so feature-detect the API with function_exists( 'wp_get_secret' ) and keep your existing storage as a fallback. Many developers also keep support for a constant defined in wp-config.php as the first choice, because agencies and hosts often prefer managing credentials there.

Migrating an existing option

If your plugin already stores a key in an option, wp_import_option_as_secret() copies it into the Secrets API. The original option is left in place on purpose, so you can verify that the secret reads back correctly before calling delete_option(). Imported secrets are automatically flagged for rotation, because the old plain-text value may already exist in older backups. A good migration therefore ends with a friendly admin notice suggesting that the site owner generates a fresh key at the provider.

Reacting to changes

The wp_secret_changed action fires when a secret is created, updated, imported, deleted or retired. It passes fingerprints rather than values, so you can safely use it for audit logs or to clear a cached connection status.

Site Health Checks for Secrets

WordPress 7.2 adds three Security tests to Site Health so site owners can see the state of their secrets at a glance:

  • Secrets API key source, which recommends action when the key is missing and turns critical if the key cannot decrypt the root key.
  • Undecryptable secrets, which is critical when any stored secret cannot be decrypted.
  • Credentials pending rotation, which is recommended when secrets are flagged, for example after an import.

The Site Health Info tab also gains a Secrets API section listing the provider, keyring, key source and the number of stored secrets. This is a helpful place to look first when a plugin reports that it cannot save its API key.

What This Means for Hosts and Agencies

Managed hosts and larger agencies often already use dedicated secret managers or hardware security modules. The Secrets API supports a wp-content/secrets.php drop-in, loaded in a similar way to db.php, that can override three components:

  • The keyring (WP_Secrets_Keyring), which protects the root key, for example using a cloud KMS.
  • The store (WP_Secrets_Store), which keeps encrypted records outside the options tables.
  • The provider (WP_Secrets_Provider), which hands the whole API to an external secret manager.

These are abstract classes, so drop-ins use extends rather than implements. A host-managed provider can also be read-only. In that case wp_secrets_provider_is_writable() returns false and plugins should show a clear notice explaining where credentials are managed. The Make WordPress Hosting team published a call to test the Secrets API on hosting platforms, and provisioning tools that write wp-config.php are encouraged to add WP_SECRETS_KEY alongside the salts.

What the Secrets API Protects, and What It Does Not

Clear expectations make any security feature more useful. The Secrets API is designed to protect credentials against database-only exposure: a leaked SQL dump, an exposed backup, a staging copy shared with a contractor or data read through an SQL injection flaw. In each of those cases the encrypted values are not useful without the key in wp-config.php.

It is not designed to isolate plugins from one another. Any code running on the site can read any secret, so choosing well-maintained plugins remains essential. It also does not protect against someone who has full access to your server files, since they could read wp-config.php as well. That is why the Secrets API works best as one layer of a broader approach, alongside the practices in our WordPress security checklist.

Core contributors are actively discussing the scope of the feature in the dev note comments, including capability checks and how platform-provided secrets should relate to application-stored ones. This kind of open review is a healthy part of how WordPress ships features. Because details can still be refined before the final release on December 8, 2026, always confirm the final behaviour in the official WordPress 7.2 release notes and field guide.

A Practical Checklist Before You Update

  1. Take a full backup of files and database, and confirm you can restore it.
  2. Test WordPress 7.2 on a staging copy first. Our WordPress 7.2 update checklist walks through the full process.
  3. After updating, open Tools → Site Health and add the suggested WP_SECRETS_KEY line to wp-config.php.
  4. Save the key in your password manager so it survives future migrations.
  5. Watch for plugin updates that adopt the API, and rotate any credentials they flag after importing.

Frequently Asked Questions

Is there an admin screen for managing secrets in WordPress 7.2?

No. WordPress 7.2 provides the developer API and Site Health checks. A management screen is planned for a later release, so for now each plugin keeps its own settings screen and simply stores the value through the API.

Will my existing plugins stop working?

No. Plugins that store keys in options continue to work exactly as before. The Secrets API is opt-in for developers, and plugins will adopt it at their own pace.

What happens if I lose WP_SECRETS_KEY?

Stored secrets become unreadable until the original value is restored. If it cannot be recovered, you would re-enter the affected API keys in each plugin. Keeping a secure copy of the key avoids this entirely.

Does the Secrets API need a specific PHP version?

It relies on the sodium functions that have been bundled with PHP since 7.2, and WordPress 7.2 keeps its minimum PHP version at 7.4. For speed and security, a newer release is still a better choice; see our guide to the best PHP version for WordPress.

Conclusion

The WordPress Secrets API is a quiet but meaningful step forward. It gives every plugin a shared, encrypted home for credentials, keeps the decryption key out of the database, and makes accidental leaks through logs much less likely. For site owners, the action item is small: add WP_SECRETS_KEY after updating and keep it safe. For developers, adopting wp_set_secret() and wp_get_secret() is a simple change that gives users stronger protection by default. As WordPress continues to connect with AI services and third-party APIs, a dependable foundation like this helps the whole ecosystem grow with confidence.

Leave a Reply

Your email address will not be published. Required fields are marked *