Opened 6 months ago
Last modified 4 months 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: | raftaar1191 | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | AI | Version: | |
| Severity: | normal | Keywords: | connectors ai-client 2nd-opinion |
| Cc: | Focuses: | sustainability |
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 (10)
This ticket was mentioned in Slack in #core-ai by raftaar1191. View the logs.
6 months ago
#3
@
6 months 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
@
6 months 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
@
5 months 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.
#8
@
4 months ago
@darshitrajyaguru97 are you working on this? if yes please let me know I can test it as well
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Proposed Function Declaration
The
has_ai_credentials()function should be declared as follows:PHP Code
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
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.