: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* REST API: WP_REST_Meta_Fields class
* Core class to manage meta values for an object via the REST API.
#[AllowDynamicProperties]
abstract class WP_REST_Meta_Fields {
* Retrieves the object meta type.
* @return string One of 'post', 'comment', 'term', 'user', or anything
* else supported by `_get_meta_table()`.
abstract protected function get_meta_type();
* Retrieves the object meta subtype.
* @return string Subtype for the meta type, or empty string if no specific subtype.
protected function get_meta_subtype() {
* Retrieves the object type for register_rest_field().
* @return string The REST field type, such as post type name, taxonomy name, 'comment', or `user`.
abstract protected function get_rest_field_type();
* Registers the meta field.
* @see register_rest_field()
public function register_field() {
_deprecated_function( __METHOD__, '5.6.0' );
$this->get_rest_field_type(),
'get_callback' => array( $this, 'get_value' ),
'update_callback' => array( $this, 'update_value' ),
'schema' => $this->get_field_schema(),
* Retrieves the meta field value.
* @param int $object_id Object ID to fetch meta for.
* @param WP_REST_Request $request Full details about the request.
* @return array Array containing the meta values keyed by name.
public function get_value( $object_id, $request ) {
$fields = $this->get_registered_fields();
foreach ( $fields as $meta_key => $args ) {
$all_values = get_metadata( $this->get_meta_type(), $object_id, $meta_key, false );
if ( empty( $all_values ) ) {
$value = $args['schema']['default'];
$value = $this->prepare_value_for_response( $value, $request, $args );
if ( is_array( $all_values ) ) {
foreach ( $all_values as $row ) {
$value[] = $this->prepare_value_for_response( $row, $request, $args );
$response[ $name ] = $value;
* Prepares a meta value for a response.
* This is required because some native types cannot be stored correctly
* in the database, such as booleans. We need to cast back to the relevant
* type before passing back to JSON.
* @param mixed $value Meta value to prepare.
* @param WP_REST_Request $request Current request object.
* @param array $args Options for the field.
* @return mixed Prepared value.
protected function prepare_value_for_response( $value, $request, $args ) {
if ( ! empty( $args['prepare_callback'] ) ) {
$value = call_user_func( $args['prepare_callback'], $value, $request, $args );
* @param array $meta Array of meta parsed from the request.
* @param int $object_id Object ID to fetch meta for.
* @return null|WP_Error Null on success, WP_Error object on failure.
public function update_value( $meta, $object_id ) {
$fields = $this->get_registered_fields();
foreach ( $fields as $meta_key => $args ) {
if ( ! array_key_exists( $name, $meta ) ) {
* A null value means reset the field, which is essentially deleting it
* from the database and then relying on the default value.
* Non-single meta can also be removed by passing an empty array.
if ( is_null( $value ) || ( array() === $value && ! $args['single'] ) ) {
$args = $this->get_registered_fields()[ $meta_key ];
$current = get_metadata( $this->get_meta_type(), $object_id, $meta_key, true );
if ( is_wp_error( rest_validate_value_from_schema( $current, $args['schema'] ) ) ) {
'rest_invalid_stored_value',
/* translators: %s: Custom field key. */
sprintf( __( 'The %s property has an invalid stored value, and cannot be updated to null.' ), $name ),
$result = $this->delete_meta_value( $object_id, $meta_key, $name );
if ( is_wp_error( $result ) ) {
$error->merge_from( $result );
if ( ! $args['single'] && is_array( $value ) && count( array_filter( $value, 'is_null' ) ) ) {
'rest_invalid_stored_value',
/* translators: %s: Custom field key. */
sprintf( __( 'The %s property has an invalid stored value, and cannot be updated to null.' ), $name ),
$is_valid = rest_validate_value_from_schema( $value, $args['schema'], 'meta.' . $name );
if ( is_wp_error( $is_valid ) ) {
$is_valid->add_data( array( 'status' => 400 ) );
$error->merge_from( $is_valid );
$value = rest_sanitize_value_from_schema( $value, $args['schema'] );
$result = $this->update_meta_value( $object_id, $meta_key, $name, $value );
$result = $this->update_multi_meta_value( $object_id, $meta_key, $name, $value );
if ( is_wp_error( $result ) ) {
$error->merge_from( $result );
if ( $error->has_errors() ) {
* Deletes a meta value for an object.
* @param int $object_id Object ID the field belongs to.
* @param string $meta_key Key for the field.
* @param string $name Name for the field that is exposed in the REST API.
* @return true|WP_Error True if meta field is deleted, WP_Error otherwise.
protected function delete_meta_value( $object_id, $meta_key, $name ) {
$meta_type = $this->get_meta_type();
if ( ! current_user_can( "delete_{$meta_type}_meta", $object_id, $meta_key ) ) {
/* translators: %s: Custom field key. */
sprintf( __( 'Sorry, you are not allowed to edit the %s custom field.' ), $name ),
'status' => rest_authorization_required_code(),
if ( null === get_metadata_raw( $meta_type, $object_id, wp_slash( $meta_key ) ) ) {
if ( ! delete_metadata( $meta_type, $object_id, wp_slash( $meta_key ) ) ) {
'rest_meta_database_error',
__( 'Could not delete meta value from database.' ),
'status' => WP_Http::INTERNAL_SERVER_ERROR,
* Updates multiple meta values for an object.
* Alters the list of values in the database to match the list of provided values.
* @param int $object_id Object ID to update.
* @param string $meta_key Key for the custom field.
* @param string $name Name for the field that is exposed in the REST API.
* @param array $values List of values to update to.
* @return true|WP_Error True if meta fields are updated, WP_Error otherwise.
protected function update_multi_meta_value( $object_id, $meta_key, $name, $values ) {
$meta_type = $this->get_meta_type();
if ( ! current_user_can( "edit_{$meta_type}_meta", $object_id, $meta_key ) ) {
/* translators: %s: Custom field key. */
sprintf( __( 'Sorry, you are not allowed to edit the %s custom field.' ), $name ),
'status' => rest_authorization_required_code(),
$current_values = get_metadata( $meta_type, $object_id, $meta_key, false );
$subtype = get_object_subtype( $meta_type, $object_id );
if ( ! is_array( $current_values ) ) {
$current_values = array();
$to_remove = $current_values;
foreach ( $to_add as $add_key => $value ) {
$remove_keys = array_keys(
function ( $stored_value ) use ( $meta_key, $subtype, $value ) {
return $this->is_meta_value_same_as_stored_value( $meta_key, $subtype, $stored_value, $value );
if ( empty( $remove_keys ) ) {
if ( count( $remove_keys ) > 1 ) {
// To remove, we need to remove first, then add, so don't touch.
$remove_key = $remove_keys[0];
unset( $to_remove[ $remove_key ] );
unset( $to_add[ $add_key ] );
* `delete_metadata` removes _all_ instances of the value, so only call once. Otherwise,
* `delete_metadata` will return false for subsequent calls of the same value.
* Use serialization to produce a predictable string that can be used by array_unique.
$to_remove = array_map( 'maybe_unserialize', array_unique( array_map( 'maybe_serialize', $to_remove ) ) );
foreach ( $to_remove as $value ) {
if ( ! delete_metadata( $meta_type, $object_id, wp_slash( $meta_key ), wp_slash( $value ) ) ) {
'rest_meta_database_error',
/* translators: %s: Custom field key. */
sprintf( __( 'Could not update the meta value of %s in database.' ), $meta_key ),
'status' => WP_Http::INTERNAL_SERVER_ERROR,
foreach ( $to_add as $value ) {
if ( ! add_metadata( $meta_type, $object_id, wp_slash( $meta_key ), wp_slash( $value ) ) ) {
'rest_meta_database_error',
/* translators: %s: Custom field key. */
sprintf( __( 'Could not update the meta value of %s in database.' ), $meta_key ),
'status' => WP_Http::INTERNAL_SERVER_ERROR,
* Updates a meta value for an object.
* @param int $object_id Object ID to update.
* @param string $meta_key Key for the custom field.
* @param string $name Name for the field that is exposed in the REST API.
* @param mixed $value Updated value.
* @return true|WP_Error True if the meta field was updated, WP_Error otherwise.
protected function update_meta_value( $object_id, $meta_key, $name, $value ) {
$meta_type = $this->get_meta_type();
// Do the exact same check for a duplicate value as in update_metadata() to avoid update_metadata() returning false.
$old_value = get_metadata( $meta_type, $object_id, $meta_key );
$subtype = get_object_subtype( $meta_type, $object_id );
if ( is_array( $old_value ) && 1 === count( $old_value )
&& $this->is_meta_value_same_as_stored_value( $meta_key, $subtype, $old_value[0], $value )
if ( ! current_user_can( "edit_{$meta_type}_meta", $object_id, $meta_key ) ) {
/* translators: %s: Custom field key. */
sprintf( __( 'Sorry, you are not allowed to edit the %s custom field.' ), $name ),
'status' => rest_authorization_required_code(),
if ( ! update_metadata( $meta_type, $object_id, wp_slash( $meta_key ), wp_slash( $value ) ) ) {
'rest_meta_database_error',
/* translators: %s: Custom field key. */
sprintf( __( 'Could not update the meta value of %s in database.' ), $meta_key ),
'status' => WP_Http::INTERNAL_SERVER_ERROR,
* Checks if the user provided value is equivalent to a stored value for the given meta key.
* @param string $meta_key The meta key being checked.
* @param string $subtype The object subtype.
* @param mixed $stored_value The currently stored value retrieved from get_metadata().
* @param mixed $user_value The value provided by the user.
protected function is_meta_value_same_as_stored_value( $meta_key, $subtype, $stored_value, $user_value ) {
$args = $this->get_registered_fields()[ $meta_key ];
$sanitized = sanitize_meta( $meta_key, $user_value, $this->get_meta_type(), $subtype );
if ( in_array( $args['type'], array( 'string', 'number', 'integer', 'boolean' ), true ) ) {
// The return value of get_metadata will always be a string for scalar types.
$sanitized = (string) $sanitized;
return $sanitized === $stored_value;
* Retrieves all the registered meta fields.
* @return array Registered fields.
protected function get_registered_fields() {
$meta_type = $this->get_meta_type();
$meta_subtype = $this->get_meta_subtype();
$meta_keys = get_registered_meta_keys( $meta_type );
if ( ! empty( $meta_subtype ) ) {
$meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $meta_type, $meta_subtype ) );
foreach ( $meta_keys as $name => $args ) {
if ( empty( $args['show_in_rest'] ) ) {
if ( is_array( $args['show_in_rest'] ) ) {
$rest_args = $args['show_in_rest'];
'single' => $args['single'],
'type' => ! empty( $args['type'] ) ? $args['type'] : null,
'prepare_callback' => array( $this, 'prepare_value' ),
'type' => $default_args['type'],
'description' => empty( $args['description'] ) ? '' : $args['description'],
'default' => isset( $args['default'] ) ? $args['default'] : null,
$rest_args = array_merge( $default_args, $rest_args );
$rest_args['schema'] = array_merge( $default_schema, $rest_args['schema'] );
$type = ! empty( $rest_args['type'] ) ? $rest_args['type'] : null;
$type = ! empty( $rest_args['schema']['type'] ) ? $rest_args['schema']['type'] : $type;
if ( null === $rest_args['schema']['default'] ) {
$rest_args['schema']['default'] = static::get_empty_value_for_type( $type );
$rest_args['schema'] = rest_default_additional_properties_to_false( $rest_args['schema'] );
if ( ! in_array( $type, array( 'string', 'boolean', 'integer', 'number', 'array', 'object' ), true ) ) {
if ( empty( $rest_args['single'] ) ) {
$rest_args['schema'] = array(