| 67 | | * Registers a new field on an existing WordPress object type. |
| 68 | | * |
| 69 | | * @since 4.4.0 |
| 70 | | * |
| 71 | | * @global array $wp_rest_additional_fields Holds registered fields, organized |
| 72 | | * by object type. |
| 73 | | * |
| 74 | | * @param string|array $object_type Object(s) the field is being registered |
| 75 | | * to, "post"|"term"|"comment" etc. |
| 76 | | * @param string $attribute The attribute name. |
| 77 | | * @param array $args { |
| 78 | | * Optional. An array of arguments used to handle the registered field. |
| 79 | | * |
| 80 | | * @type string|array|null $get_callback Optional. The callback function used to retrieve the field |
| 81 | | * value. Default is 'null', the field will not be returned in |
| 82 | | * the response. |
| 83 | | * @type string|array|null $update_callback Optional. The callback function used to set and update the |
| 84 | | * field value. Default is 'null', the value cannot be set or |
| 85 | | * updated. |
| 86 | | * @type string|array|null $schema Optional. The callback function used to create the schema for |
| 87 | | * this field. Default is 'null', no schema entry will be returned. |
| 88 | | * } |
| 89 | | */ |
| 90 | | function register_api_field( $object_type, $attribute, $args = array() ) { |
| 91 | | $defaults = array( |
| 92 | | 'get_callback' => null, |
| 93 | | 'update_callback' => null, |
| 94 | | 'schema' => null, |
| 95 | | ); |
| 96 | | |
| 97 | | $args = wp_parse_args( $args, $defaults ); |
| 98 | | |
| 99 | | global $wp_rest_additional_fields; |
| 100 | | |
| 101 | | $object_types = (array) $object_type; |
| 102 | | |
| 103 | | foreach ( $object_types as $object_type ) { |
| 104 | | $wp_rest_additional_fields[ $object_type ][ $attribute ] = $args; |
| 105 | | } |
| 106 | | } |
| 107 | | |
| 108 | | /** |