Crest Design
WordPress 4 min read

WordPress Secrets API: encrypting plugin API keys in 7.2

The WordPress Secrets API in 7.2 encrypts the API keys your plugins use. How it works, WP_SECRETS_KEY setup, plugin code, handover tips and common fixes.

Crest Design
Cover for a guide to encrypting plugin API keys with the WordPress 7.2 Secrets API.

If your WordPress site talks to a payment gateway, a CRM, an email platform or an AI service, there's an API key sitting somewhere in it. In most sites we inherit, that key is stored as a plain database option, readable by anyone who gets a copy of the database. This guide is for site owners and the developers who look after their sites: it explains the new WordPress Secrets API in WordPress 7.2, how it encrypts the API keys your plugins use, what you need to set up before relying on it, and what to ask your developer or agency to change.

The change is recent. The core team published the Secrets API dev note in November 2026, and WordPress 7.2, the release that ships it, is scheduled for 8 December 2026. Plugins can start using it from that release, so now is a good time to plan.

The problem: API keys stored in plain sight

Here's how a client usually describes it: "We gave our old developer the Stripe and HubSpot keys, they pasted them into a settings page, and we have no idea where they live now." That's a fair worry. Until now, WordPress had no shared, built-in way to store a credential safely, so each plugin made its own choice. Many saved keys with update_option(), which writes them to the wp_options table as readable text.

That causes real problems:

  • Database copies leak keys. Backups, staging clones and exports sent to a freelancer all carry every key in readable form.
  • Rotation is risky. Changing a key means pasting a new one and hoping nothing still uses the old one.
  • Handover is messy. When you change agencies, nobody is sure which keys exist, where they're stored, or who can see them.

Some developers move keys into wp-config.php constants instead. That keeps them out of the database, but it means a site owner can't update a key without file access, and it doesn't scale when several plugins each need their own.

How the WordPress Secrets API works

The Secrets API gives plugins and themes one shared place to keep credentials, encrypted at rest. The idea is simple: the stored value lives in the database, but the key that decrypts it lives outside the database, in wp-config.php. Someone holding only a database copy holds only ciphertext.

How the WordPress 7.2 Secrets API saves an API key encrypted with WP_SECRETS_KEY and reveals it only when the plugin calls an outside service.
Saving and reading a key with the WordPress 7.2 Secrets API

The moving parts

  • WP_SECRETS_KEY is a constant in wp-config.php that WordPress uses to encrypt and decrypt secrets. According to the dev note, new installs are set up with one, and Site Health tells existing sites how to add it. Core won't write it into your config file during an upgrade, so on existing sites your host or developer adds it.
  • wp_set_secret( $name, $value ) encrypts a value and stores it under a name. Names are prefixed by the plugin, such as acme-crm/api-key. The prefix stops plugins overwriting each other by accident; it isn't a permission system.
  • wp_get_secret( $name ) reads it back. It returns a WP_Secret object that stays masked until code calls reveal(), which keeps the raw key out of debug output and logs by default.
  • Versions. Each secret keeps its previous version, so you can rotate a key without breaking code that's mid-request.

Why reads have three possible answers

This is the part most worth understanding, because it shapes how plugins should behave. wp_get_secret() can return a WP_Secret (use it), null (the secret was never stored), or a WP_Error (the secret exists but can't be read right now, for example because WP_SECRETS_KEY is missing or the database is unavailable).

The three results of wp_get_secret in WordPress 7.2: a WP_Secret to use, null when never saved, or a WP_Error when saved but unreadable.
Handle all three answers from wp_get_secret() differently

The difference between null and an error matters. If a plugin treats both as "no key" and asks the admin to paste one again, a temporary config problem turns into an overwritten credential and a confused client.

Two limits in 7.2 are good to know. There's no admin screen for secrets yet; that's planned for a later release. And the Connectors screen, where you add AI provider keys, still stores its keys as ordinary settings in 7.2. Moving Connectors onto the Secrets API is planned follow-up work.

How to encrypt API keys in WordPress with the Secrets API

Here's the order we'd follow on a live site.

  1. 1 Confirm WP_SECRETS_KEY exists. After updating to WordPress 7.2, open Tools > Site Health. If the key is missing, Site Health says so. On managed hosting, ask your host to add it; many will handle it for every site on the platform.
  2. 2 List every stored credential. Search plugin settings and wp_options for API keys, tokens and webhook secrets. Write down which plugin owns each and which outside service it unlocks.
  3. 3 Update plugins that support the API. Plugin authors will add support over time. Check changelogs for "Secrets API" before assuming a key has moved.
  4. 4 Change custom code. Anything your developer wrote that calls update_option() for a key should move to wp_set_secret(), with a fallback for older WordPress versions.
  5. 5 Rotate after migrating. Once a key is stored encrypted, generate a fresh one at the provider and save it. The old readable copy may still exist in older backups.

The wp-config.php line looks like this. Use a long random value from your host or developer, in the format the dev note describes, and never commit it to Git:

// wp-config.php, above "That's all, stop editing!"
define( 'WP_SECRETS_KEY', 'paste-the-generated-value-here' );

And here's a minimal pattern for a custom plugin that saves a CRM key and uses it. It goes in your plugin's main file or an included class; change the acme-crm prefix and the endpoint to your own.

<?php
// Save a key from your settings form handler (after nonce and capability checks).
function acme_save_api_key( string $raw_key ) {
    if ( ! function_exists( 'wp_set_secret' ) ) {
        return new WP_Error( 'acme_no_secrets_api', 'This needs WordPress 7.2 or later.' );
    }
    if ( ! wp_secrets_provider_is_writable() ) {
        return new WP_Error( 'acme_read_only', 'Secrets on this site are managed by the host.' );
    }
    return wp_set_secret( 'acme-crm/api-key', sanitize_text_field( $raw_key ) );
}

// Read the key only at the moment you call the outside service.
function acme_crm_request( string $path ) {
    $secret = wp_get_secret( 'acme-crm/api-key' );

    if ( null === $secret ) {
        return new WP_Error( 'acme_no_key', 'Add your CRM API key in Settings.' );
    }
    if ( is_wp_error( $secret ) ) {
        error_log( 'CRM key unreadable: ' . $secret->get_error_code() );
        return $secret; // Don't ask the admin to re-enter it.
    }

    return wp_remote_get( 'https://api.example-crm.com' . $path, array(
        'headers' => array( 'Authorization' => 'Bearer ' . $secret->reveal() ),
        'timeout' => 10,
    ) );
}

Notice that the code never logs the revealed value and never stores it in a variable longer than the request needs. WordPress 7.2 was still in release candidates when this was written, so your developer should confirm the final function signatures against the 7.2 reference before shipping.

What we've learned so far

Working through the release candidates and the dev note, a few points stand out.

  • Encryption at rest isn't access control. Any code running on the site, including every plugin, can call wp_get_secret(). Fewer, well-maintained plugins still matter.
  • The key file is now precious. If WP_SECRETS_KEY is lost, stored secrets can't be decrypted. Back up wp-config.php separately from the database, and store it securely.
  • Staging needs thought. A staging site with the same WP_SECRETS_KEY can read production secrets. We'd give staging its own key and its own test credentials from the provider.
  • Constants still have a place. On sites where the client never edits keys and everything deploys from Git, environment variables or constants remain a reasonable choice. The Secrets API fits best where admins manage keys through a settings screen.

Troubleshooting common problems

Symptom Likely cause Fix
Saving a key returns a secret_key_unavailable error WP_SECRETS_KEY isn't defined Add the constant to wp-config.php or ask your host, then save again
Keys stop working after a migration to new hosting The new wp-config.php has a different or missing secrets key Copy the original WP_SECRETS_KEY across, or re-enter each key and rotate it
A plugin keeps asking for a key that was saved The plugin treats a read error as "no key" Check the error log, fix the key constant, and report the behaviour to the plugin author
Saving fails only on one host The host manages secrets and the store is read-only Set the key through the host's dashboard or ask their support
Staging sends real emails or charges Staging shares production's key and secrets Give staging its own secrets key and sandbox credentials

Questions people ask

Do I need to do anything when WordPress 7.2 arrives?

Check Site Health for the secrets key after you update. Nothing moves automatically: plugins that store keys today keep doing so until their authors adopt the API. Your developer should review custom code.

Is the Secrets API enough for compliance?

It's a sound technical step, but whether it meets a specific rule depends on your situation. If you handle payment or personal data, check with a qualified adviser.

Can my agency still see our keys?

Anyone with admin-level code access to the site can, in principle, read a secret through code. Use provider-side controls too: restricted keys, scoped permissions and rotation when an agency's access ends.

Why it matters for your business

API keys connect your site to money, customer data and paid services. A leaked key can mean unexpected charges, data exposure or a broken checkout, and it often shows up first in an old backup or a forgotten staging copy. Encrypting keys at rest, rotating them on a schedule and knowing where each one lives makes your site easier to hand over and safer to share with the people who work on it. It also fits naturally into the update routine we described for WordPress automatic updates and what to check after each release, and the 7.2 testing work from our WordPress jQuery 4 upgrade guide.

A short checklist for the 7.2 update:

  • Confirm WP_SECRETS_KEY in Site Health.
  • List every API key and the plugin that uses it.
  • Move custom code to wp_set_secret() with a fallback.
  • Give staging its own key and credentials.
  • Rotate keys once they're stored encrypted.

For the full technical detail, read the official Secrets API dev note on Make WordPress Core.

If you'd like help reviewing how your site stores its keys, our WordPress development service covers it, or you can tell us about your site.

(over to you)

Planning a build like this?

Tell us what you're building and we'll come back with ideas and next steps.