register_meta() – Registers a meta key.

You appear to be a bot. Output may be restricted

Description

Registers a meta key.

It is recommended to register meta keys for a specific combination of object type and object subtype. If passing an object subtype is omitted, the meta key will be registered for the entire object type, however it can be partly overridden in case a more specific meta key of the same name exists for the same object type and a subtype. If an object type does not support any subtypes, such as users or comments, you should commonly call this function without passing a subtype.

Usage

$bool = register_meta( $object_type, $meta_key, $args, $deprecated );

Parameters

$object_type
( string ) required – Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', or any other object type with an associated meta table.
$meta_key
( string ) required – Meta key to register.
$args
( array ) required – { Data used to describe the meta key when registered.
$object_subtype
( string ) required – A subtype; e.g. if the object type is "post", the post type. If left empty, the meta key will be registered on the entire object type. Default empty.
$type
( string ) required – The type of data associated with this meta key. Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
$description
( string ) required – A description of the data attached to this meta key.
$single
( bool ) required – Whether the meta key has one value per object, or an array of values per object.
$default
( mixed ) required – The default value returned from get_metadata() if no value has been set yet. When using a non-single meta key, the default value is for the first entry. In other words, when calling get_metadata() with $single set to `false`, the default value given here will be wrapped in an array.
$sanitize_callback
( callable ) required – A function or method to call when sanitizing $meta_key data.
$auth_callback
( callable ) required – Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks.
$show_in_rest
( bool|array ) required – Whether data associated with this meta key can be considered public and should be accessible via the REST API. A custom post type must also declare support for custom fields for registered meta to be accessible via REST. When registering complex meta values this argument may optionally be an array with 'schema' or 'prepare_callback' keys instead of a boolean. }
$deprecated
( string|array ) optional – Deprecated. Use $args instead.

Returns

bool True if the meta key was successfully registered in the global array, false if not. Registering a meta key with distinct sanitize and auth callbacks will fire those callbacks, but will not add to the global registry.

Source

File name: wordpress/wp-includes/meta.php
Lines:

101 to 118 of 118
    }

    if ( ! has_filter( "default_{$object_type}_metadata", 'filter_default_metadata' ) ) {
      add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 );
    }
  }

  // Global registry only contains meta keys registered with the array of arguments added in 4.6.0.
  if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
    unset( $args['object_subtype'] );

    $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;

    return true;
  }

  return false;
}
 

 View on GitHub View on Trac