wppaste
WordPress

register_theme_feature( string $feature, array $args = array() ): true|WP_Error

Since
5.5.0
Source
wp-includes/theme.php:3281
Registers a theme feature for use in add_theme_support().

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()
  • $typestring

    The type of data associated with this feature. Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'. Defaults to 'boolean'.
  • $variadicbool

    Does 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.
  • $descriptionstring

    A 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.
  • $schemaarray

    Specifies the JSON Schema definition describing the feature. If any objects in the schema do not include the 'additionalProperties' keyword, it is set to false.
  • $namestring

    An alternate name to be used as the property name in the REST API.
  • $prepare_callbackcallable

    A 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

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
28–132

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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.

WhenInstructionsCalls it makes
always28–31wp_parse_args(), __(), wp_parse_args()
always32–61wp_parse_args()
always34–63wp_parse_args(), __()
always39–42wp_parse_args(), schema(), __(), wp_parse_args()
always43–72wp_parse_args(), schema()
always45–74wp_parse_args(), schema(), __()
isset($value) && !is_callable()50–79wp_parse_args(), is_callable(), __(), sprintf()
isset($value) && !is_callable()61–90wp_parse_args(), schema(), is_callable(), __(), sprintf()
!isset($value)64–100wp_parse_args(), description(), rest_default_additional_properties_to_false()
isset($value) && is_callable()70–106wp_parse_args(), is_callable(), description(), rest_default_additional_properties_to_false()
!isset($value)75–111wp_parse_args(), schema(), description(), rest_default_additional_properties_to_false()
isset($value) && is_callable()81–117wp_parse_args(), schema(), is_callable(), description(), rest_default_additional_properties_to_false()
4 further outcomes, up to 132 instructions
!isset($value)86–115wp_parse_args(), description(), array_unshift(), rest_default_additional_properties_to_false()
isset($value) && is_callable()92–121wp_parse_args(), is_callable(), description(), array_unshift(), rest_default_additional_properties_to_false()
!isset($value)97–126wp_parse_args(), schema(), description(), array_unshift(), rest_default_additional_properties_to_false()
isset($value) && is_callable()103–132wp_parse_args(), schema(), is_callable(), description(), array_unshift(), rest_default_additional_properties_to_false()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev18428–13219
8.518428–13219
8.418428–132193 fewer instructions than PHP 8.3
8.318728–13519
8.218728–13519
8.118728–13519
7.418728–13519

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

Used by · 1

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.