A small brass and black tin robot holds up one brass key in front of a pegboard of keys on separate hooks.

Where WordPress Stores AI API Keys, and Where They Should Live

WordPress 7.0 added a single place to connect AI providers: Settings → Connectors. Paste in a key from Anthropic, Google, or OpenAI, and AI features across the site can use it.

That key is worth protecting. Anyone holding it can spend your money and send data in your name. Where WordPress keeps it decides how far it travels: into backups, staging copies, and every clone of the site.

The Three Places WordPress Looks

WordPress checks for a connector’s key in this order and uses the first one it finds:

  1. An environment variable set on the server
  2. A PHP constant, usually defined in wp-config.php
  3. The database, which is where a key typed into Settings → Connectors goes

By default, the name is the connector’s ID in capitals plus _API_KEY, such as ANTHROPIC_API_KEY or OPENAI_API_KEY. Connectors can declare other names, so make sure to check each connector’s documentation.

To see where your keys live, open Settings → Connectors. It shows each connector’s key source and whether it’s connected.

Why the Database Is the Weakest Option

The settings screen is the easiest path and the default. It also has three drawbacks.

  • Plain text. Keys saved there are stored unencrypted. The official AI plugin’s Key Encryption experiment encrypts them at rest, but turning the experiment off or deactivating the plugin puts them back in plain text.
  • They travel. A database key goes into every backup, every export, and every staging clone. Anyone who can open one of those can read it.
  • They can vanish. The AI plugin’s 1.4.0 changelog notes that core deletes a stored key when its revalidation request times out, and raises that timeout from 5 to 30 seconds.

A wp-config.php constant can’t be deleted that way and stays out of database exports, but it still sits in plain text in a file that file backups copy. A server-level environment variable stays out of a typical files-and-database backup altogether.

How to Move a Key Out of the Database

As a constant. Replace the placeholder with your key, then add the line to wp-config.php above the “That’s all, stop editing!” comment:

As an environment variable. On servers running PHP-FPM, add this to the site’s pool file, replacing the placeholder. The file’s location varies by server, and PHP-FPM needs a reload afterward. Some managed hosts offer their own way to set environment variables; check with yours.

Then reload Settings → Connectors to confirm the new source. Finally, replace the key itself: create a new one at the provider for the new location and revoke the old one, because the old key still sits in past backups.

One Key Per Site, and No Key Forever

Give each site its own key. If one leaks or a feature misbehaves, you can revoke it without breaking every other site, and each site’s usage is easy to trace. Anthropic, Google, and OpenAI all let you set up a project or workspace per site with its own spending cap; see How to Cap AI Spending for Your WordPress Site.

Give staging its own key, too. Copying a live site to staging copies any key stored in its database. Environment variables avoid this: staging gets its own value, and nothing travels with the clone.

Rotate on a schedule. No key should live forever. To rotate without downtime, create the new key, update it on the site, confirm AI features still work, then revoke the old one. And rotate when staff or vendors change.

Know which plugins use the shared key. Plugins that bring their own AI keys don’t appear in Settings → Connectors, so track those separately. The AI plugin’s Connector Approvals experiment can require admin approval before a plugin or theme uses the connectors configured on your site.

This field is for validation purposes and should be left unchanged.
WordPress Responsible AI Checklist

Sources

(Last Updated: October 30, 2026)


This post was developed in collaboration with Claude Opus 5.5. Image generated using ChatGPT and Adobe Firefly via Photoshop. The final content was edited, tested, formatted, and fact-checked by Erik Lutenegger.

Read Other Technical Snippets

All Technical Snippets

[email protected]

Not sure where to start when setting up AI for WordPress?