Title: register_rest_field
Published: December 7, 2016
Last modified: August 20, 2026

---

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

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#wp--skip-link--target)

Registers a new field on an existing WordPress object type.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#parameters)󠁿

 `$object_type`string|arrayrequired

Object(s) the field is being registered to, "post"|"term"|"comment" etc.

`$attribute`stringrequired

The attribute name.

`$args`arrayoptional

An array of arguments used to handle the registered field.

 * `get_callback` callable|null
 * Optional. The callback function used to retrieve the field value. Default is `'
   null'`, the field will not be returned in the response. The function will be 
   passed the prepared object data.
 * `update_callback` callable|null
 * Optional. The callback function used to set and update the field value. Default
   is `'null'`, the value cannot be set or updated. The function will be passed 
   the model object, like [WP_Post](https://developer.wordpress.org/reference/classes/wp_post/).
 * `schema` array|null
 * Optional. The schema for this field.
    Default is `'null'`, no schema entry will
   be returned.

Default:`array()`

## 󠀁[Source](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#source)󠁿

    ```php
    function register_rest_field( $object_type, $attribute, $args = array() ) {
    	global $wp_rest_additional_fields;

    	$defaults = array(
    		'get_callback'    => null,
    		'update_callback' => null,
    		'schema'          => null,
    	);

    	$args = wp_parse_args( $args, $defaults );

    	$object_types = (array) $object_type;

    	foreach ( $object_types as $object_type ) {
    		$wp_rest_additional_fields[ $object_type ][ $attribute ] = $args;
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rest-api.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/rest-api.php#L182)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/rest-api.php#L182-L198)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_parse_args()](https://developer.wordpress.org/reference/functions/wp_parse_args/)`wp-includes/functions.php` |

Merges user defined arguments into defaults array.

  |

| Used by | Description | 
| [WP_REST_Meta_Fields::register_field()](https://developer.wordpress.org/reference/classes/wp_rest_meta_fields/register_field/)`wp-includes/rest-api/fields/class-wp-rest-meta-fields.php` |

Registers the meta field.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.7.0](https://developer.wordpress.org/reference/since/4.7.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 9 content](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#comment-content-2260)
 2.    [Matt Barrett (corradomatt)](https://profiles.wordpress.org/corradomatt/)  [  9 years ago  ](https://developer.wordpress.org/reference/functions/register_rest_field/#comment-2260)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-2260)
     Vote results for this note: 9[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-2260)
 4.  GET callbacks accept up to 4 arguments in order…
 5.   * $post – (object) Post or custom post type object of the request.
      * $attr – (string) Rest field/attr string identifier from the second parameter
        of your `register_rest_field()` declaration.
      * $request – (object) Full request payload – as a [WP_REST_Request](https://developer.wordpress.org/reference/classes/wp_rest_request/)
        object
      * $object_type – (string) The object type which the field is registered against.
        Typically first parameter of your `register_rest_field()` declaration.
 6.  Post callbacks accept the above arguments but also a 5th argument in the first
     position which is the value being passed from the POST request to the attribute/
     field.
 7.   * The order for update callback is not correct (anymore?). As of March 2018, 
        WordPress 4.9.4 the order of arguments in **update_callback**: $metaValue, 
        $object, $metaKey.
      * [aepp](https://profiles.wordpress.org/aepp/) [9 years ago](https://developer.wordpress.org/reference/functions/register_rest_field/#comment-2615)
      * The first argument in the get is post array, not object. Thanks for this tip,
        I was looking on how to add REST request and this saved me tons of time.
      * [Denis Žoljom](https://profiles.wordpress.org/dingo_d/) [6 years ago](https://developer.wordpress.org/reference/functions/register_rest_field/#comment-4150)
      * The fourth parameter is no longer accepted.
      * [Mark Howells-Mead](https://profiles.wordpress.org/markhowellsmead/) [4 years ago](https://developer.wordpress.org/reference/functions/register_rest_field/#comment-6001)
 8.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%3Freplytocom%3D2260%23feedback-editor-2260)
 9.   [Skip to note 10 content](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#comment-content-3434)
 10.   [Manzoor Wani](https://profiles.wordpress.org/manzoorwanijk/)  [  7 years ago  ](https://developer.wordpress.org/reference/functions/register_rest_field/#comment-3434)
 11. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-3434)
     Vote results for this note: 8[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-3434)
 12. To make use of `sanitize_callback` and `validate_callback`, pass those in `arg_options`
     in `schema`
 13.     ```php
         // Make sure to use PHP >= 5.4
         add_action(
         	'rest_api_init',
         	function () {
         		// Field name to register.
         		$field = 'my_field';
         		register_rest_field(
         			'post',
         			$field,
         			array(
         				'get_callback'    => function ( $object ) use ( $field ) {
         					// Get field as single value from post meta.
         					return get_post_meta( $object['id'], $field, true );
         				},
         				'update_callback' => function ( $value, $object ) use ( $field ) {
         					// Update the field/meta value.
         					update_post_meta( $object->ID, $field, $value );
         				},
         				'schema'          => array(
         					'type'        => 'string',
         					'arg_options' => array(
         						'sanitize_callback' => function ( $value ) {
         							// Make the value safe for storage.
         							return sanitize_text_field( $value );
         						},
         						'validate_callback' => function ( $value ) {
         							// Valid if it contains exactly 10 English letters.
         							return (bool) preg_match( '/\A[a-z]{10}\Z/', $value );
         						},
         					),
         				),
         			)
         		);
         	}
         );
         ```
     
 14.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%3Freplytocom%3D3434%23feedback-editor-3434)
 15.  [Skip to note 11 content](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#comment-content-2145)
 16.   [Nate Finch](https://profiles.wordpress.org/finchps/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/register_rest_field/#comment-2145)
 17. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-2145)
     Vote results for this note: 5[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-2145)
 18. An example to add all of the post meta to a `post-meta-fields` field:
 19.     ```php
         add_action( 'rest_api_init', 'create_api_posts_meta_field' );
     
         function create_api_posts_meta_field() {
     
         	// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
         	register_rest_field( 'post', 'post-meta-fields', array(
         	       'get_callback'    => 'get_post_meta_for_api',
         	       'schema'          => null,
         	    )
         	);
         }
     
         function get_post_meta_for_api( $object ) {
         	//get the id of the post object array
         	$post_id = $object['id'];
     
         	//return the post meta
          	return get_post_meta( $post_id );
         }
         ```
     
 20. This can be extended or edited. For example, if you don’t want to include all 
     the post meta, you can filter it, or return only the field you need. The options
     are limitless, but the callback gives you access to the post object, which exposes
     the `id` among other parameters, so you can use many different helper functions.
 21.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%3Freplytocom%3D2145%23feedback-editor-2145)
 22.  [Skip to note 12 content](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#comment-content-2616)
 23.   [aepp](https://profiles.wordpress.org/aepp/)  [  9 years ago  ](https://developer.wordpress.org/reference/functions/register_rest_field/#comment-2616)
 24. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-2616)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-2616)
 25. This code works (also with shipped Backbone client) in WordPress **4.9.4**:
 26.     ```php
         $meta = [
             'key' => 'my_post_type_name',
             'description' => 'This is the name of my post type',
             'type' => 'string'
         ];
     
         \register_rest_field('my_post_type', $meta['key'], [
             'get_callback' => function ($params) use ($meta) {
                 return \get_post_meta($params['id'], $meta['key'], true);
             },
             'update_callback' => function ($value, $object, $fieldName){
                 return \update_post_meta($object->ID, $fieldName, $value);
             }
         ]);
         ```
     
 27.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%3Freplytocom%3D2616%23feedback-editor-2616)
 28.  [Skip to note 13 content](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#comment-content-3065)
 29.   [Aamer Shahzad](https://profiles.wordpress.org/talentedaamer/)  [  8 years ago  ](https://developer.wordpress.org/reference/functions/register_rest_field/#comment-3065)
 30. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-3065)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-3065)
 31. An example to add post featured image src to the ‘featured_image_src’ field.
 32.     ```php
         add_action( 'rest_api_init', function () {
         	register_rest_field( 'post', 'featured_image_src', array(
         		'get_callback' => function ( $post_arr ) {
         			$image_src_arr = wp_get_attachment_image_src( $post_arr['featured_media'], 'medium' );
     
         			return $image_src_arr[0];
         		},
         		'update_callback' => null,
         		'schema' => null
         	) );
         } );
         ```
     
 33. Helpful for showing featured image in gutenberg block development when posts are
     fetched with withSelect() function.
 34.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%3Freplytocom%3D3065%23feedback-editor-3065)
 35.  [Skip to note 14 content](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#comment-content-2737)
 36.   [mymonoo](https://profiles.wordpress.org/mymonoo/)  [  8 years ago  ](https://developer.wordpress.org/reference/functions/register_rest_field/#comment-2737)
 37. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-2737)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-2737)
 38. Custom sql queries can be achieved to get whatever value you want from database.
     The following queries shows how can we modify rest output to show something from
     our custom table ‘wp_it_job_details’ which is, of course, inside WordPress database.
     Put it in your themes/plugins functions.php
 39.     ```php
         function add_last_date_data() {
           //Last date
           register_rest_field( 'post',
             'last_date',
             array(
                 'get_callback'  => 'rest_get_last_date',
                 'update_callback'   => null,
                 'schema'            => null,
              )
           );
         }
         function rest_get_last_date( $object ) {
             //get the Post Id
             $post_id = $object['id'];
         	global $wpdb;
         	$sql = "SELECT * FROM wp_it_job_details WHERE post_id = '$post_id'"; //wp_it_job_details is job table
         	$results = $wpdb->get_row($sql);
         		if(count($results) > 0) {
         			//We have this post_id row in the table
         			return $results->last_date;	//last_date is the column name, change to your's
     
         		} else return ""; //return nothing 
     
     
         }
         add_action( 'rest_api_init', 'add_last_date_data' );
         ```
     
 40.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%3Freplytocom%3D2737%23feedback-editor-2737)
 41.  [Skip to note 15 content](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#comment-content-7443)
 42.   [Anthony Eden](https://profiles.wordpress.org/anthonyeden/)  [  9 months ago  ](https://developer.wordpress.org/reference/functions/register_rest_field/#comment-7443)
 43. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-7443)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-7443)
 44. The _$object\_type_ parameter is a little unclear from this description unless
     you go digging or understand the broader context of how the REST API handles object
     types (_string|array required Object(s) the field is being registered to, “post”
     |”term”|”comment” etc._).
 45. If you are registering an additional REST field for a specific taxonomy’s terms,
     simply enter the name of the taxonomy. For example, this registers the field ‘
     website’ for the taxonomy named ‘post-author’.
 46.     ```php
         add_action('rest_api_init', function() {
             register_rest_field('post-author', 'website', array(
                 'get_callback' => function($term) {
                     return 'https://example.com/...'; // Insert your actual logic to fetch the field here
                 },
             ));
         });
         ```
     
 47.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%3Freplytocom%3D7443%23feedback-editor-7443)
 48.  [Skip to note 16 content](https://developer.wordpress.org/reference/functions/register_rest_field/?output_format=md#comment-content-2091)
 49.   [systemosa](https://profiles.wordpress.org/systemosa/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/register_rest_field/#comment-2091)
 50. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-2091)
     Vote results for this note: -6[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%23comment-2091)
 51.     ```php
         // assume there custom post_type named events 
         add_action( 'rest_api_init', function() {
             register_rest_field( 'events', 'event_date', array(
     
                 'get_callback' =&gt; function( $comment_arr ) {
                     $comment_obj = get_field('event_date', $comment_arr['id'] );
                     return $comment_obj;
                 },
     
                 'update_callback' =&gt; function( $karma, $comment_obj ) {
                     update_field('event_date', $karma, $comment_arr['id'] );
                     return true;
                 },
     
                 'schema' =&gt; array(
                     'event_date' =&gt; __( 'event_date' ),
                     'type'        =&gt; 'text'
                 ),
     
             ));
     
         });
         ```
     
 52.  * This **update_callback** returns an error, since **$comment_obj** is an instance
        of **\[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)**.
        It should be instead $comment_obj->ID.
      * [aepp](https://profiles.wordpress.org/aepp/) [9 years ago](https://developer.wordpress.org/reference/functions/register_rest_field/#comment-2617)
 53.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F%3Freplytocom%3D2091%23feedback-editor-2091)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_rest_field%2F)
before being able to contribute a note or feedback.