wppaste
WordPress

WP_Connector_Registry::register( string $id, array $args ): array|null

Since
7.0.0
Source
wp-includes/class-wp-connector-registry.php:149
Registers a new connector.

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.
  • $namestring

    Required. The connector's display name.
  • $descriptionstringdefault: empty string

    Optional. The connector's description.
  • $logo_urlstring

    Optional. URL to the connector's logo image.
  • $typestring

    Required. The connector type, e.g. 'ai_provider'.
  • $authenticationarray

    { Required. Authentication configuration.
  • $methodstring

    Required. The authentication method: 'api_key', 'application_password', or 'none'.
  • $credentials_urlstring

    Optional. URL where users can obtain API credentials.
  • $settingstring

    name 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_password for 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
26–163

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 334.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksdo_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.

WhenInstructionsCalls 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

PHPCompiledExecutedBranchesNotes
8.6-dev33426–16334
8.533426–16334
8.433426–163346 fewer instructions than PHP 8.3
8.334029–16934
8.234029–16934
8.134029–169341 fewer instruction than PHP 7.4
7.434129–17034

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

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.

  1. 7.0.4
  2. 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.