Opened 8 weeks ago
Last modified 13 days ago
#64924 new enhancement
Add a `has_ai_credentials()` function to WordPress core that plugins can use to check if AI credentials are available.
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | trunk |
| Component: | AI | Keywords: | connectors ai-client 2nd-opinion |
| Focuses: | sustainability | Cc: |
Description
Summary
Add a has_ai_credentials() function to WordPress core that plugins can use to check if AI credentials are available.
Description
Currently, the has_ai_credentials() function exists in the WordPress AI plugin but should be moved to core so that all plugins can reliably check for the availability of AI credentials without having to implement their own logic.
This is a common need for plugins that integrate with AI features but should only expose certain functionality when credentials are actually configured.
Proposed Solution
Add the following function to WordPress core (recommended location: wp-includes/ai.php):
/**
* Checks if we have AI credentials set.
*
* @since 7.0
*
* @return bool True if we have AI credentials, false otherwise.
*/
function has_ai_credentials(): bool {
if ( ! function_exists( 'wp_get_connectors' ) ) {
return false;
}
foreach ( wp_get_connectors() as $connector_data ) {
if ( 'ai_provider' !== $connector_data['type'] ) {
continue;
}
$auth = $connector_data['authentication'];
if ( 'api_key' !== $auth['method'] || empty( $auth['setting_name'] ) ) {
continue;
}
if ( '' === get_option( $auth['setting_name'], '' ) ) {
continue;
}
return true;
}
/**
* Filters whether AI credentials are available.
*
* Allows third-party plugins to declare credential availability for
* connectors that do not rely on API key settings.
*
* @since 7.0
*
* @param bool $has_credentials Whether AI credentials are available.
* @param array $connector_data The connector data.
*/
return (bool) apply_filters( 'wp_has_ai_credentials', false, $connector_data );
}
Benefits
- Provides a standard, core API for checking AI credential availability
- Eliminates the need for plugins to implement their own credential-checking logic
- Ensures consistency across the WordPress ecosystem
- Allows the AI plugin to use core functionality rather than duplicating code
Current Implementation
This function currently exists in the WordPress AI plugin:
https://github.com/WordPress/ai/blob/32c5e2ddb435f30ddc40b48635c084de25846b5b/includes/helpers.php#L304
Related Tickets
Part of the broader effort to expose AI APIs in WordPress core.
Acceptance Criteria
- Function is added to core
- Function is properly documented with inline PHPDoc
- Function is exported in the public API
- Tests are added for various scenarios (no credentials, credentials present, filter usage)
- Documentation is updated
Change History (8)
This ticket was mentioned in Slack in #core-ai by raftaar1191. View the logs.
8 weeks ago
#3
@
8 weeks ago
nice suggestion, @raftaar1191 do you mean https://core.trac.wordpress.org/browser/trunk/src/wp-includes/ai-client.php location?
Can we also add has_valid_ai_credentials, if not already there.
https://github.com/WordPress/ai/blob/trunk/includes/helpers.php#L336
#4
@
8 weeks ago
Hi @lakshmananphp,
I believe the most appropriate place for this function would be within the connectors.php file in core:
https://core.trac.wordpress.org/browser/trunk/src/wp-includes/connectors.php
This seems aligned with the existing structure and responsibilities of that file.
#7
@
2 weeks ago
Adds has_ai_credentials() to core in wp-includes/connectors.php.
- Implements credential detection via
wp_get_connectors() - Supports API key-based authentication
- Includes
wpai_has_ai_credentialsfilter for extensibility - Adds defensive checks for connector structure
- Includes unit tests for:
- No connectors
- Missing credentials
- Valid credentials
- Filter override
This aligns with the Connectors API and avoids duplicating logic currently present in the AI plugin.
Note: The filter passes the computed $has_credentials value rather than a hardcoded false, allowing partial detection logic to be preserved while still enabling overrides.
Proposed Function Declaration
The
has_ai_credentials()function should be declared as follows:PHP Code
/** * Checks if we have AI credentials set. * * This function iterates through registered connectors and checks for valid * API key credentials. It supports the WordPress Connectors API and provides * a filter hook for third-party plugins to declare credential availability * for custom authentication methods. * * @since 7.0 * * @return bool True if we have AI credentials, false otherwise. * * @example * if ( has_ai_credentials() ) { * // AI provider is configured and ready * } * * @see wpai_has_ai_credentials Filter for third-party credential declarations. */ function has_ai_credentials(): bool { if ( ! function_exists( 'wp_get_connectors' ) ) { return false; } $connectors = wp_get_connectors(); foreach ( $connectors as $connector_data ) { if ( 'ai_provider' !== $connector_data['type'] ) { continue; } $auth = $connector_data['authentication']; if ( 'api_key' !== $auth['method'] || empty( $auth['setting_name'] ) ) { continue; } if ( '' === get_option( $auth['setting_name'], '' ) ) { continue; } return true; } /** * Filters whether AI credentials are available. * * Allows third-party plugins to declare credential availability for * connectors that do not rely on API key settings. * * @since 7.0 * * @param bool $has_credentials Whether AI credentials are available. * @param array $connectors The registered connectors. */ return (bool) apply_filters( 'wpai_has_ai_credentials', false, $connectors ); }Function Details
has_ai_credentials()boolwp_get_connectors()function (WordPress 6.6+)What This Function Does
Checks if valid AI provider credentials are configured through the WordPress Connectors API. Iterates through registered connectors and validates API key authentication settings.
Usage
if ( has_ai_credentials() ) { // AI provider is configured and ready to use }Filter Hook
wpai_has_ai_credentials — Allows third-party plugins to declare credential availability for custom authentication methods that don't rely on API key settings.