WordPress at a glance
Home All Function Functions APIs REST API

register_rest_field() WP 1.0

Registers a new field on an existing WordPress object type.

Works based on: wp_parse_args()
✈ 1 time = 0.000001s = speed of light | 50000 times = 0.04s = speed of light | PHP 7.1.11, WP 4.9.8

No Hooks.

Return

Null. Nothing.

Usage

register_rest_field( $object_type, $attribute, $args );
$object_type(string/array) (required)
Object(s) the field is being registered to, "post"|"term"|"comment" etc.
$attribute(string) (required)
The attribute name.
$args(array)

An array of arguments used to handle the registered field.

  • $get_callback (string/array/null)
    Optional. The callback function used to retrieve the field value.
    Default: 'null', the field will not be returned in the response

  • $update_callback (string/array/null)
    Optional. The callback function used to set and update the field value.
    Default: 'null', the value cannot be set or updated

  • $schema (string/array/null)
    Optional. The callback function used to create the schema for this field.
    Default: 'null', no schema entry will be returned

Default: array()

Notes

  • Global. Array. $wp_rest_additional_fields Holds registered fields, organized by object type.

Changelog

Since 4.7.0 Introduced.

Code of register rest field: wp-includes/rest-api.php WP 5.2.4

<?php
function register_rest_field( $object_type, $attribute, $args = array() ) {
	$defaults = array(
		'get_callback'    => null,
		'update_callback' => null,
		'schema'          => null,
	);

	$args = wp_parse_args( $args, $defaults );

	global $wp_rest_additional_fields;

	$object_types = (array) $object_type;

	foreach ( $object_types as $object_type ) {
		$wp_rest_additional_fields[ $object_type ][ $attribute ] = $args;
	}
}

Related Functions

From tag: REST API

No comments
    Hello, !     Log In . Register