register_term_meta()WP 4.9.8

Registers a meta-field for the specified taxonomy.

This is a wrapper for the function register_meta() to make it easier and clearer to register a meta-field for a taxonomy.

To do the same for a post type, see register_post_meta().

1 time — 0.000001 sec (speed of light) | 50000 times — 0.26 sec (very fast) | PHP 7.1.11, WP 4.9.8

No Hooks.

Returns

true|false.
true - if the meta key was successfully registered in the global array $wp_meta_keys.
false - if the key could not be registered.

Usage

register_term_meta( $taxonomy, $meta_key, $args );
$taxonomy(string) (required)
The taxonomy for which the meta-field is registered. Pass an empty string to register the meta key for all existing taxonomies.
$meta_key(string) (required)
The name of the key to be registered.
$args(array) (required)

Data describing the meta-field. By default, these are:

$args = array(
	'type'              => 'string',
	'description'       => '',
	'single'            => false,
	'sanitize_callback' => null,
	'auth_callback'     => null,
	'show_in_rest'      => false,
);

Description of each parameter can be found in the function register_meta().

Examples

1

#1 Registration of a meta-field for the taxonomy shelves

register_term_meta( 'shelves', 'my_meta_key', [
	'show_in_rest'      => true,     // Add to the REST response
	'sanitize_callback' => 'absint', // Process the value of the field when saving it to the database, absint()
] );

You can do the same with register_meta(), like this:

register_meta( 'term', 'my_meta_key', [
	'object_subtype'    => 'shelves', // taxonomy
	'show_in_rest'      => true,
	'sanitize_callback' => 'absint',
] );
0

#2 Extended value of the show_in_rest attribute

Using this function with schema in the show_in_rest attribute. Adds this field to the REST query and also describes this field in the OPTIONS REST query.

register_term_meta( 'replica', 'nice_field', [
	'type'        => 'string',
	'description' => 'a nice description',
	'single'       => true,
	'show_in_rest' => [
		'schema' => [
			'type'     => 'string',
			'format'   => 'url',
			'context'  => [ 'view', 'edit' ],
			'readonly' => true,
	   ]
	],
] );

Changelog

Since 4.9.8 Introduced.

register_term_meta() code WP 6.9.1

function register_term_meta( $taxonomy, $meta_key, array $args ) {
	$args['object_subtype'] = $taxonomy;

	return register_meta( 'term', $meta_key, $args );
}