register_rest_field

Registers a new field on an existing WordPress object type.

Description

register_rest_field( (string|array) $object_type, (string) $attribute, (array) $args = array() ); 

Parameters (3)

0. $object_type (string|array)
Object(s) the field is being registered to, post|"term"|"comment" etc.
1. $attribute (string)
The attribute name.
2. $args — Optional. (array) => array()
An array of arguments used to handle the registered field.

Options

  • get_callback (string|array|null) => 'null'

    The callback function used to retrieve the field value.

  • update_callback (string|array|null) => 'null'

    The callback function used to set and update the field value.

array(

    /**
     * Optional. The callback function used to retrieve the field value.
     *
     * @type string|array|null
     * @default 'null'
     * @optional
     */
    'get_callback' => 'null',

    /**
     * Optional. The callback function used to set and update the field value.
     *
     * @type string|array|null
     * @default 'null'
     * @optional
     */
    'update_callback' => 'null'
);        


Usage

  1. if ( !function_exists( 'register_rest_field' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/vendor/wp-rest-functions.php'; 
  3.  
  4. // Object(s) the field is being registered 
  5. // to, "post"|"term"|"comment" etc. 
  6. $object_type = null; 
  7.  
  8. // The attribute name. 
  9. $attribute = ''; 
  10.  
  11. // Optional. An array of arguments used to handle the registered field. 
  12. $args = array( 
  13. 'get_callback' => 'null', 
  14. 'update_callback' => 'null' 
  15. ); 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = register_rest_field($object_type, $attribute, $args); 
  19.  

Defined (1)

The function is defined in the following location(s).

/includes/vendor/wp-rest-functions.php  
  1. function register_rest_field( $object_type, $attribute, $args = array() ) { 
  2. $defaults = array( 
  3. 'get_callback' => null,  
  4. 'update_callback' => null,  
  5. 'schema' => null,  
  6. ); 
  7.  
  8. $args = wp_parse_args( $args, $defaults ); 
  9.  
  10. global $wp_rest_additional_fields; 
  11.  
  12. $object_types = (array) $object_type; 
  13.  
  14. foreach ( $object_types as $object_type ) { 
  15. $wp_rest_additional_fields[ $object_type ][ $attribute ] = $args;