Skip to content

when running register_meta on an array, it needs to be passed the schema too. #34

@LC43

Description

@LC43

There's a notice in when calling register_meta:

Caution

"register_meta was called incorrectly. When registering an "array" meta type to show in the REST API, you must specify the schema for each array item in "show_in_rest.schema.items"

The docs say we can either pass schema or prepare_callback:

show_in_rest bool|array:

When registering complex meta values this argument may optionally be an array with 'schema' or 'prepare_callback' keys instead of a boolean.
from register_meta

meaning:

'show_in_rest' => array(
  'schema' => array(
    'type'  => 'array',
    'items' => array(
      'type' => 'number',
    ),
  ),
),

or

src/Features/Meta/CodestarMeta.php:135

register_meta(
	'post',
	$meta_name,
	array(
		'object_subtype' => $post_type,
		'type'           => $rest_type,
		'single'         => true,
		'show_in_rest'   => [
			'prepare_callback' => function ( $value ) {
				return wp_json_encode( $value );
			},
		],
	)
);

or

'list_of_objects',
array(
	'single'       => false,
	'type'         => 'array',
        'show_in_rest' => array(
        	'schema' => array(
        		'type'  => 'array',
        		'items' => array(
        			'type'       => 'object',
        			'properties' => array(
        				'version' => array(
        					'type' => 'string',
        				),
        				'artist'  => array(
        					'type' => 'string',
        				),
        			),
        		),
        	),
        ),
),

Note

but! it only checks for schema.items

https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/meta.php#L1475

if ( false !== $args['show_in_rest'] && 'array' === $args['type'] ) {
	if ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'When registering an "array" meta type to show in the REST API, you must specify the schema for each array item in "show_in_rest.schema.items".' ), '5.3.0' );

		return false;
	}
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions