[wp-trac] [WordPress Trac] #64924: Add a `has_ai_credentials()` function to WordPress core that plugins can use to check if AI credentials are available.

WordPress Trac noreply at wordpress.org
Sun Mar 22 19:39:26 UTC 2026


#64924: Add a `has_ai_credentials()` function to WordPress core that plugins can
use to check if AI credentials are available.
-------------------------+--------------------------------------------
 Reporter:  raftaar1191  |       Owner:  (none)
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  AI           |     Version:  trunk
 Severity:  normal       |  Resolution:
 Keywords:  2nd-opinion  |     Focuses:  accessibility, sustainability
-------------------------+--------------------------------------------
Changes (by raftaar1191):

 * keywords:  2nd-opinion needs-patch needs-testing => 2nd-opinion


Comment:

 == Proposed Function Declaration ==

 The `has_ai_credentials()` function should be declared as follows:

 === PHP Code ===

 {{{
 #!php
 /**
  * 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 ===

 * '''Function Name:''' `has_ai_credentials()`
 * '''Return Type:''' `bool`
 * '''Since:''' WordPress 7.0
 * '''Requires:''' `wp_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 ===

 {{{
 #!php
 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.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/64924#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list