WP_Block_Bindings_Registry::register( string $source_name, array $source_properties ): WP_Block_Bindings_Source|false
- Since
- 6.5.0
- Source
wp-includes/class-wp-block-bindings-registry.php:84
Description
This is a low-level method. For most use cases, it is recommended to use the register_block_bindings_source() function instead.
Compatibility
- WordPress
- since 6.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$source_namestring- The name of the source. It must be a string containing a namespace prefix, i.e.
my-plugin/my-custom-source. It must only contain lowercase alphanumeric characters, the forward slash/and dashes. $source_propertiesarray- The array of arguments that are used to register a source.
$labelstringThe label of the source.$get_value_callbackcallableA callback executed when the source is processed during block rendering. The callback should have the following signature:function( $source_args, $block_instance, $attribute_name ): mixed- @param array $source_args Array containing source arguments used to look up the override value, i.e. {"key": "foo"}. - @param WP_Block $block_instance The block instance. - @param string $attribute_name The name of the target attribute. The callback has a mixed return type; it may return a string to override the block's original value, null, false to remove an attribute, etc.$uses_contextstring[]Optional. Array of values to add to blockuses_contextneeded by the source.
Return value
WP_Block_Bindings_Source|false- Source when the registration was successful, or
falseon failure.
Performance profile
How much work a call to WP_Block_Bindings_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
- 13–42
- 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 118.
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_Block_Bindings_Registry::register()
Further down the call graph this can also reach query, option, cache, serialize 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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Block_Bindings_Registry::register() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 13–15 | __(), _doing_it_wrong() |
!->is_registered() && !isset($source_properties) | 21–23 | ->is_registered(), __(), _doing_it_wrong() |
->is_registered() | 23 | ->is_registered(), __(), sprintf(), _doing_it_wrong() |
!->is_registered() && isset($source_properties) | 28–33 | ->is_registered(), is_callable(), __(), _doing_it_wrong() |
!->is_registered() && isset($source_properties) && is_callable() && !array_diff() | 39–42 | ->is_registered(), is_callable(), array_keys(), array_diff() |
!->is_registered() && isset($source_properties) && is_callable() && array_diff() | 39–42 | ->is_registered(), is_callable(), array_keys(), array_diff(), __(), _doing_it_wrong() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 118 | 13–42 | 9 | |
| 8.5 | 118 | 13–42 | 9 | |
| 8.4 | 118 | 13–42 | 9 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 124 | 16–48 | 9 | |
| 8.2 | 124 | 16–48 | 9 | |
| 8.1 | 124 | 16–48 | 9 | |
| 7.4 | 124 | 16–48 | 9 |
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 · 4
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- WP_Block_Bindings_Registry::is_registered()Checks if a block bindings source is registered.
- WP_Block_Bindings_Source::__construct()Constructor.
Source code
public function register( string $source_name, array $source_properties ) { if ( ! is_string( $source_name ) ) { _doing_it_wrong( __METHOD__, __( 'Block bindings source name must be a string.' ), '6.5.0' ); return false; } if ( preg_match( '/[A-Z]+/', $source_name ) ) { _doing_it_wrong( __METHOD__, __( 'Block bindings source names must not contain uppercase characters.' ), '6.5.0' ); return false; } $name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/'; if ( ! preg_match( $name_matcher, $source_name ) ) { _doing_it_wrong( __METHOD__, __( 'Block bindings source names must contain a namespace prefix. Example: my-plugin/my-custom-source' ), '6.5.0' ); return false; } if ( $this->is_registered( $source_name ) ) { _doing_it_wrong( __METHOD__, /* translators: %s: Block bindings source name. */ sprintf( __( 'Block bindings source "%s" already registered.' ), $source_name ), '6.5.0' ); return false; } // Validates that the source properties contain the label. if ( ! isset( $source_properties['label'] ) ) { _doing_it_wrong( __METHOD__, __( 'The $source_properties must contain a "label".' ), '6.5.0' ); return false; } // Validates that the source properties contain the get_value_callback. if ( ! isset( $source_properties['get_value_callback'] ) ) { _doing_it_wrong( __METHOD__, __( 'The $source_properties must contain a "get_value_callback".' ), '6.5.0' ); return false; } // Validates that the get_value_callback is a valid callback. if ( ! is_callable( $source_properties['get_value_callback'] ) ) { _doing_it_wrong( __METHOD__, __( 'The "get_value_callback" parameter must be a valid callback.' ), '6.5.0' ); return false; } // Validates that the uses_context parameter is an array. if ( isset( $source_properties['uses_context'] ) && ! is_array( $source_properties['uses_context'] ) ) { _doing_it_wrong( __METHOD__, __( 'The "uses_context" parameter must be an array.' ), '6.5.0' ); return false; } if ( ! empty( array_diff( array_keys( $source_properties ), $this->allowed_source_properties ) ) ) {Changelog
Introduced in 6.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-block-bindings-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.