register_theme_feature( string $feature, array $args = array() ): true|WP_Error
- Since
- 5.5.0
- Source
wp-includes/theme.php:3281
Description
This does not indicate that the active theme supports the feature, it only describes the feature's supported options.
Compatibility
- WordPress
- since 5.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
$featurestring- The name uniquely identifying the feature. See add_theme_support() for the list of possible values.
$argsarrayoptional- Data used to describe the theme.Default:
array()$typestringThe type of data associated with this feature. Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'. Defaults to 'boolean'.$variadicboolDoes this feature utilize the variadic support of add_theme_support(), or are all arguments specified as the second parameter. Must be used with the "array" type.$descriptionstringA short description of the feature. Included in the Themes REST API schema. Intended for developers.$show_in_restbool|array{ Whether this feature should be included in the Themes REST API endpoint. Defaults to not being included. When registering an 'array' or 'object' type, this argument must be an array with the 'schema' key.$schemaarraySpecifies the JSON Schema definition describing the feature. If any objects in the schema do not include the 'additionalProperties' keyword, it is set to false.$namestringAn alternate name to be used as the property name in the REST API.$prepare_callbackcallableA function used to format the theme support in the REST API. Receives the raw theme support value. }
Return value
true|WP_Error- True if the theme feature was successfully registered, a WP_Error object if not.
Performance profile
How much work a call to register_theme_feature() 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
- 28–132
- Plugin surface
- None
- Called by
- 1
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 184.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What one call costs · 16 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost register_theme_feature() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 28–31 | wp_parse_args(), __(), wp_parse_args() |
| always | 32–61 | wp_parse_args() |
| always | 34–63 | wp_parse_args(), __() |
| always | 39–42 | wp_parse_args(), schema(), __(), wp_parse_args() |
| always | 43–72 | wp_parse_args(), schema() |
| always | 45–74 | wp_parse_args(), schema(), __() |
isset($value) && !is_callable() | 50–79 | wp_parse_args(), is_callable(), __(), sprintf() |
isset($value) && !is_callable() | 61–90 | wp_parse_args(), schema(), is_callable(), __(), sprintf() |
!isset($value) | 64–100 | wp_parse_args(), description(), rest_default_additional_properties_to_false() |
isset($value) && is_callable() | 70–106 | wp_parse_args(), is_callable(), description(), rest_default_additional_properties_to_false() |
!isset($value) | 75–111 | wp_parse_args(), schema(), description(), rest_default_additional_properties_to_false() |
isset($value) && is_callable() | 81–117 | wp_parse_args(), schema(), is_callable(), description(), rest_default_additional_properties_to_false() |
4 further outcomes, up to 132 instructions
!isset($value) | 86–115 | wp_parse_args(), description(), array_unshift(), rest_default_additional_properties_to_false() |
isset($value) && is_callable() | 92–121 | wp_parse_args(), is_callable(), description(), array_unshift(), rest_default_additional_properties_to_false() |
!isset($value) | 97–126 | wp_parse_args(), schema(), description(), array_unshift(), rest_default_additional_properties_to_false() |
isset($value) && is_callable() | 103–132 | wp_parse_args(), schema(), is_callable(), description(), array_unshift(), rest_default_additional_properties_to_false() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 184 | 28–132 | 19 | |
| 8.5 | 184 | 28–132 | 19 | |
| 8.4 | 184 | 28–132 | 19 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 187 | 28–135 | 19 | |
| 8.2 | 187 | 28–135 | 19 | |
| 8.1 | 187 | 28–135 | 19 | |
| 7.4 | 187 | 28–135 | 19 |
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
- wp_parse_args()Merges user defined arguments into defaults array.
- __()Retrieves the translation of $text.
- rest_default_additional_properties_to_false()Sets the "additionalProperties" to false by default for all object definitions in the schema.
- WP_Error::__construct()Initializes the error.
Used by · 1
- create_initial_theme_features()Creates the initial theme features when the 'setup_theme' action is fired.
Source code
function register_theme_feature( $feature, $args = array() ) { global $_wp_registered_theme_features; if ( ! is_array( $_wp_registered_theme_features ) ) { $_wp_registered_theme_features = array(); } $defaults = array( 'type' => 'boolean', 'variadic' => false, 'description' => '', 'show_in_rest' => false, ); $args = wp_parse_args( $args, $defaults ); if ( true === $args['show_in_rest'] ) { $args['show_in_rest'] = array(); } if ( is_array( $args['show_in_rest'] ) ) { $args['show_in_rest'] = wp_parse_args( $args['show_in_rest'], array( 'schema' => array(), 'name' => $feature, 'prepare_callback' => null, ) ); } if ( ! in_array( $args['type'], array( 'string', 'boolean', 'integer', 'number', 'array', 'object' ), true ) ) { return new WP_Error( 'invalid_type', __( 'The feature "type" is not valid JSON Schema type.' ) ); } if ( true === $args['variadic'] && 'array' !== $args['type'] ) { return new WP_Error( 'variadic_must_be_array', __( 'When registering a "variadic" theme feature, the "type" must be an "array".' ) ); } if ( false !== $args['show_in_rest'] && in_array( $args['type'], array( 'array', 'object' ), true ) ) { if ( ! is_array( $args['show_in_rest'] ) || empty( $args['show_in_rest']['schema'] ) ) { return new WP_Error( 'missing_schema', __( 'When registering an "array" or "object" feature to show in the REST API, the feature\'s schema must also be defined.' ) ); } if ( 'array' === $args['type'] && ! isset( $args['show_in_rest']['schema']['items'] ) ) { return new WP_Error( 'missing_schema_items', __( 'When registering an "array" feature, the feature\'s schema must include the "items" keyword.' ) ); } if ( 'object' === $args['type'] && ! isset( $args['show_in_rest']['schema']['properties'] ) ) { return new WP_Error( 'missing_schema_properties', __( 'When registering an "object" feature, the feature\'s schema must include the "properties" keyword.' ) ); } } if ( is_array( $args['show_in_rest'] ) ) { if ( isset( $args['show_in_rest']['prepare_callback'] ) && ! is_callable( $args['show_in_rest']['prepare_callback'] ) ) { return new WP_Error( 'invalid_rest_prepare_callback', sprintf( /* translators: %s: prepare_callback */ __( 'The "%s" must be a callable function.' ), 'prepare_callback' ) );Changelog
Introduced in 5.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/theme.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.