If you need to restrict which WordPress roles can configure AI API keys, the short answer is to use a dedicated capability instead of a role name. Grant that capability to administrators by default, then add it to a custom role or another role only when a trusted user truly needs to manage provider credentials. The WordPress wp-ai-client discussion recommends a custom capability mapped to a Core capability, limited to admins by default because only admins should configure AI provider credentials [1].
This guide is for site owners, developers, and technically curious administrators who run AI features on WordPress. It covers the capability model, a practical implementation pattern, plugin-based alternatives, verification steps, and the tradeoffs you should understand before you delegate API key access.
Why API key configuration needs its own permission
AI API keys are not ordinary settings. They are credentials that can spend money, access provider accounts, and expose usage data. A user who can change an API key can redirect requests to another account, replace a key with an invalid one, or silently consume a budget. Aimogen’s role-restriction documentation makes the same practical point: AI features are powerful and expensive, and if every logged-in user can trigger generation or change prompts, predictability is lost [2].
WordPress already has a permission system for this problem. Roles are named collections of capabilities, and capabilities are the individual permissions that code checks. Administrators normally have manage_options, which controls access to most site settings. Editors, authors, contributors, and subscribers do not. The mistake is to treat manage_options as the only possible gate. It works, but it is broad: every administrator gets access, and you cannot easily grant AI key management to one non-admin without giving them a much larger set of permissions.
Understand the capability layer before you write a rule
A capability check answers one question: can this user do this specific action? WordPress core provides capabilities such as edit_posts, publish_posts, and manage_options. Plugins can register their own capabilities. That is the recommended path for AI provider settings.
Two ideas are easy to confuse:
- Using AI features means generating text, images, or other output through an AI provider. This may be appropriate for editors or authors in a controlled workflow.
- Configuring AI API keys means entering, replacing, or deleting provider credentials and changing provider-level settings. This should normally remain with administrators or a very small trusted group.
The wp-ai-client issue separates these concerns in its discussion: it asks how to control which users can use AI, while noting that only admins can configure the AI provider credentials [1]. Your implementation should do the same. Create one capability for using AI and a separate capability for managing API keys.
The recommended pattern: a dedicated capability
The following pattern is a general WordPress implementation. It is not tied to one AI plugin, but the plugin that stores the API keys must check the capability. If the plugin hardcodes manage_options or another broad capability, you may need to ask the plugin author for a filter or a custom capability.
1. Choose a clear capability name
Use a name that describes the action, not the role. For example:
manage_ai_api_keysAvoid names like editor_ai_access because they tie permissions to a role. Roles can change, and custom roles may not match the built-in ones.
2. Grant the capability to administrators by default
When your plugin or site-specific code is activated, add the capability to the administrator role. This keeps the default secure: administrators can configure keys, and no one else can until you explicitly allow it.
function myplugin_add_ai_capability() { $admin = get_role( 'administrator' ); if ( $admin ) { $admin->add_cap( 'manage_ai_api_keys' ); } } register_activation_hook( __FILE__, 'myplugin_add_ai_capability' );If the capability already exists, add_cap() is safe to call again. On deactivation, you may choose to remove it, but be careful: removing a capability can lock users out of settings they still need.
3. Check the capability on the settings page
Hiding a menu item is not access control. A user can type the settings URL directly. Check the capability before rendering the page and before saving any field.
if ( ! current_user_can( 'manage_ai_api_keys' ) ) { wp_die( esc_html__( 'You do not have permission to configure AI API keys.', 'my-plugin' ) ); }For form submissions, repeat the check in the handler. A common mistake is to check only when the page loads and then trust the submitted form.
4. Protect REST API and AJAX endpoints
Modern AI plugins often use the WordPress REST API or admin-ajax.php. Both need a permission check. For a REST route, use a permission_callback:
register_rest_route( 'my-plugin/v1', '/settings', array( 'methods' => 'POST', 'callback' => 'myplugin_save_settings', 'permission_callback' => function() { return current_user_can( 'manage_ai_api_keys' ); }, ) );For admin-ajax.php, check current_user_can() inside the handler before processing the request. Do not rely on a nonce alone. A nonce verifies intent; it does not prove that the user has the required capability.
5. Grant the capability to another role only when needed
If a trusted editor must manage API keys, add the capability to that role. You can do this in code:
$editor = get_role( 'editor' ); if ( $editor ) { $editor->add_cap( 'manage_ai_api_keys' ); }This is a significant decision. The editor can now change provider credentials. If the goal is only to let editors use AI, grant the separate use_ai_features capability instead. Keep the API key capability with a smaller group.
Using a role-management plugin instead of code
If you do not want to edit code, role-management plugins can assign capabilities to roles. The important limitation is that they can only assign a capability that already exists. The AI plugin must register the capability and check it. If the AI plugin only checks manage_options, a role-management plugin cannot create a narrower gate by itself.
When you use a role-management plugin, document the change. Capability changes are invisible in the normal WordPress admin unless you look at the role editor. A future administrator may not know why an editor can see AI provider settings. Also test the change with a real low-privilege account, not only with an administrator account.
Plugin example: Aimogen role restrictions
Aimogen, an all-in-one AI WordPress plugin, documents role-based controls in its own settings. According to its documentation, you can find options under Limits & Statistics > AI Usage Limit and restrict plugin menus under Plugin Menu Restrictions [2]. The same documentation describes controls for global access, content generation inside the editor, bulk and automated actions, prompt editing and system instructions, image generation and media creation, and API budget or usage limits by role [2].
This is a useful example of a plugin that exposes role restrictions in the admin UI. It does not mean every AI plugin works this way. If your AI plugin does not offer role controls, you may need to use its hooks, a custom capability, or a small companion plugin. Check the plugin’s documentation and source before assuming that a setting exists.
What to restrict, and what to leave alone
A practical permission model separates four actions:
- Configure provider credentials: administrators or a custom ‘AI manager’ role only.
- Change global AI settings: administrators or the same small group.
- Use AI generation: editors, authors, or a custom role, depending on workflow.
- Manage budgets and usage limits: administrators or a finance/operations role if one exists.
If you cannot create custom roles, you can still use capabilities. For example, grant use_ai_features to the editor role, but keep manage_ai_api_keys with administrators. That gives editors the tool without giving them the credentials.
Verification: test with a low-privilege account
The following is an illustrative test protocol. It is not a report of a specific test run, but it describes how to verify your own setup.
- Create a test user with the Editor role.
- Log in as that user and look for the AI settings menu. It should be absent if the capability is not granted.
- Visit the settings page URL directly. Expected result: WordPress denies access, or your custom message appears.
- Submit a settings form or call the REST endpoint using the editor’s session. Expected result: a 403 response or a permission error.
- Grant
manage_ai_api_keysto the editor role using code or a role-management plugin. - Repeat steps 2 and 3. Expected result: the menu appears and the page loads.
- Revoke the capability and repeat again. Expected result: access is denied.
If the editor can still reach the settings after revoking the capability, check for caching, a second capability check, or a plugin that grants the capability elsewhere. If the settings page loads but saving fails, check the form handler and the REST permission callback separately.
Limitations, tradeoffs, and edge cases
A dedicated capability is only as good as the plugin’s checks. If the AI plugin always checks manage_options, you cannot narrow access without modifying the plugin or asking the author for a filter. The wp-ai-client issue shows that this is an active design question in the WordPress AI ecosystem, not a solved default in every plugin [1].
Multisite adds another layer. Network administrators may have different capabilities from site administrators, and API keys may be network-wide or per-site. Test the exact configuration you plan to use.
Finally, consider where the key is stored. If the key is in the database and editable through the admin, capability checks are the main protection. If the key is defined in wp-config.php or an environment variable, the admin UI may not need to expose it at all. That can be a stronger operational choice for sites with many administrators, but it also means non-developers cannot rotate the key without server access.
Next action
Decide who truly needs to configure AI API keys. In most WordPress sites, that group is one or two administrators. Create a dedicated capability, grant it to administrators by default, and use a separate capability for AI usage. If you delegate key management, delegate it to a custom role rather than a broad built-in role. Then verify the result with a low-privilege test account.
If you are working through this on a specific plugin, share your WordPress version, AI plugin, role setup, and the exact permission error in the community. That information makes it easier to compare notes and identify whether the limitation is in WordPress, the plugin, or the server configuration.
FAQ
Can I restrict AI API key settings by checking for the administrator role?
Checking a role name is brittle. Use a capability instead. Administrators can be granted a dedicated capability by default, and other roles can receive it without changing their role name.
Is hiding the AI settings menu enough?
No. Direct URL access, form submissions, AJAX, and REST requests must also check the capability server-side.
How do I let editors use AI without letting them change API keys?
Create two capabilities: one for using AI features and one for managing provider credentials. Grant only the usage capability to editors.
Does Aimogen support role-based restrictions for AI features?
Yes. Aimogen's documentation describes role-based controls under Limits & Statistics > AI Usage Limit and Plugin Menu Restrictions, including access, generation, automation, prompt editing, image generation, and budget limits [2].
What if my AI plugin only checks manage_options?
You cannot narrow access through role management alone. Ask the plugin author for a custom capability or filter, or use a companion plugin that enforces the check before the AI plugin runs.