WP_Connector_Registry::register( string $id, array $args ): array|null
- Since
- 7.0.0
- Source
wp-includes/class-wp-connector-registry.php:149
Description
Validates the provided arguments and stores the connector in the registry.
For connectors with api_key or application_password authentication, a setting_name can be provided explicitly. When omitted, setting names are automatically generated using the pattern connectors_{$type}_{$id}_{$method}, with hyphens in the type and ID normalized to underscores. These setting names are used for Settings API registration and REST API exposure.
Registering a connector with an ID that is already registered will trigger a _doing_it_wrong() notice and return null. To override an existing connector, call unregister() first.
Compatibility
- WordPress
- since 7.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 2 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$idstring- The unique connector identifier. Must match the pattern
/^[a-z0-9_-]+$/(lowercase alphanumeric, hyphens, and underscores only). $argsarray- An associative array of arguments for the connector.
$namestringRequired. The connector's display name.$descriptionstringdefault: empty stringOptional. The connector's description.$logo_urlstringOptional. URL to the connector's logo image.$typestringRequired. The connector type, e.g. 'ai_provider'.$authenticationarray{ Required. Authentication configuration.$methodstringRequired. The authentication method: 'api_key', 'application_password', or 'none'.$credentials_urlstringOptional. URL where users can obtain API credentials.$settingstringname Optional. The setting name for the API key or application-password credentials. When omitted, auto-generated as `connectors{$type}_{$id}_apikeyfor API keys andconnectors{$type}_{$id}_application_passwordfor application passwords. Must be a non-empty string when provided. @type string $constant_name Optional. PHP constant name for the API key (e.g. 'ANTHROPIC_API_KEY') or for application-password credentials inusername:passwordformat. Only checked when provided. @type string $env_var_name Optional. Environment variable name for the API key (e.g. 'ANTHROPIC_API_KEY') or for application-password credentials inusername:passwordformat. Only checked when provided. } @type array $plugin { Optional. Plugin data for install/activate UI. @type string $file Optional. The plugin's main file path relative to the plugins directory (e.g. 'my-plugin/my-plugin.php' or 'hello.php'). @type callable $is_active Optional callback to determine whether the plugin is active. Receives no arguments and must return bool. Defaults to__return_true`. }
Return value
array|null- The registered connector data on success, null on failure.
Performance profile
How much work a call to WP_Connector_Registry::register() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 26–163
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 334.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()one call below WP_Connector_Registry::register()
Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Connector_Registry::register() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$id && !->is_registered() | 26–134 | ->is_registered(), __(), esc_html(), sprintf(), _doing_it_wrong() |
$id && !->is_registered() && !empty($args) && isset($args) && !empty($value) | 63–151 | ->is_registered() |
$id && !->is_registered() && !empty($args) && !empty($value) && wp_supports_ai() | 66–144 | ->is_registered(), wp_supports_ai() |
$id && !->is_registered() && !empty($args) && !empty($value) && wp_supports_ai() && isset($value) | 81–127 | ->is_registered(), wp_supports_ai(), __(), esc_html(), sprintf(), _doing_it_wrong() |
$id && !->is_registered() && !empty($args) && isset($args) && !empty($value) && isset($value) && is_callable() | 83–162 | ->is_registered(), is_callable() |
$id && !->is_registered() && !empty($args) && !empty($value) && wp_supports_ai() && isset($value) && is_callable() | 86–155 | ->is_registered(), wp_supports_ai(), is_callable() |
$id && !->is_registered() && !empty($args) && isset($args) && !empty($value) && isset($value) && !is_callable() | 87–163 | ->is_registered(), is_callable(), __(), esc_html(), sprintf(), _doing_it_wrong() |
$id && !->is_registered() && !empty($args) && !empty($value) && wp_supports_ai() && isset($value) && !is_callable() | 90–156 | ->is_registered(), wp_supports_ai(), is_callable(), __(), esc_html(), sprintf(), _doing_it_wrong() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 334 | 26–163 | 34 | |
| 8.5 | 334 | 26–163 | 34 | |
| 8.4 | 334 | 26–163 | 34 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 340 | 29–169 | 34 | |
| 8.2 | 340 | 29–169 | 34 | |
| 8.1 | 340 | 29–169 | 34 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 341 | 29–170 | 34 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 5
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- esc_html()Escaping for HTML blocks.
- wp_supports_ai()Returns whether AI features are supported in the current environment.
- WP_Connector_Registry::is_registered()Checks if a connector is registered.
Source code
public function register( string $id, array $args ): ?array { if ( ! preg_match( '/^[a-z0-9_-]+$/', $id ) ) { _doing_it_wrong( __METHOD__, __( 'Connector ID must contain only lowercase alphanumeric characters, hyphens, and underscores.' ), '7.0.0' ); return null; } if ( $this->is_registered( $id ) ) { _doing_it_wrong( __METHOD__, /* translators: %s: Connector ID. */ sprintf( __( 'Connector "%s" is already registered.' ), esc_html( $id ) ), '7.0.0' ); return null; } // Validate required fields. if ( empty( $args['name'] ) || ! is_string( $args['name'] ) ) { _doing_it_wrong( __METHOD__, /* translators: %s: Connector ID. */ sprintf( __( 'Connector "%s" requires a non-empty "name" string.' ), esc_html( $id ) ), '7.0.0' ); return null; } if ( empty( $args['type'] ) || ! is_string( $args['type'] ) ) { _doing_it_wrong( __METHOD__, /* translators: %s: Connector ID. */ sprintf( __( 'Connector "%s" requires a non-empty "type" string.' ), esc_html( $id ) ), '7.0.0' ); return null; } if ( ! isset( $args['authentication'] ) || ! is_array( $args['authentication'] ) ) { _doing_it_wrong( __METHOD__, /* translators: %s: Connector ID. */ sprintf( __( 'Connector "%s" requires an "authentication" array.' ), esc_html( $id ) ), '7.0.0' ); return null; } if ( empty( $args['authentication']['method'] ) || ! in_array( $args['authentication']['method'], array( 'api_key', 'application_password', 'none' ), true ) ) { _doing_it_wrong( __METHOD__, /* translators: %s: Connector ID. */ sprintf( __( 'Connector "%s" authentication method must be "api_key", "application_password", or "none".' ), esc_html( $id ) ), '7.0.0' ); return null; } if ( 'ai_provider' === $args['type'] && ! wp_supports_ai() ) { // No need for a `doing_it_wrong` as AI support is disabled intentionally. return null; } $connector = array( 'name' => $args['name'], 'description' => isset( $args['description'] ) && is_string( $args['description'] ) ? $args['description'] : '', 'type' => $args['type'], 'authentication' => array( 'method' => $args['authentication']['method'], ), ); if ( ! empty( $args['logo_url'] ) && is_string( $args['logo_url'] ) ) { $connector['logo_url'] = $args['logo_url']; }Changelog
Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.
Signature, return type and hooks compared across 2 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-connector-registry.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.