Modify WordPress REST Endpoint Response

in REST API

When working with the WordPress REST API you can get all the information back you need for a specific post. This data will consist of all the standard information such as:

  • Slug
  • Title
  • Content
  • Excerpt
  • Featured images
[{
	"id": 1111,
	"date": "",
	"date_gmt": "",
	"guid": {
		"rendered": ""
	},
	"modified": "",
	"modified_gmt": "",
	"slug": "",
	"type": "post",
	"link": "",
	"title": {
		"rendered": ""
	},
	"content": {
		"rendered": "",
		"protected": false
	},
	"excerpt": {
		"rendered": "",
		"protected": false
	},
	"author": 1,
	"featured_media": 1111,
	"comment_status": "open",
	"ping_status": "closed",
	"sticky": false,
	"template": "",
	"format": "standard",
	"meta": {
		"_edd_button_behavior": []
	},
	"categories": [],
	"tags": [],
	"_links": {
		"self": [{
			""
		}],
		"collection": [{
			"s"
		}],
		"about": [{
			""
		}],
		"author": [{
			"embeddable": true,
			"href": ""
		}],
		"replies": [{
			"embeddable": true,
			"href": ""
		}],
		"version-history": [{
			"href": ""
		}],
		"wp:featuredmedia": [{
			"embeddable": true,
			"href": ""
		}],
		"wp:attachment": [{
			"href": ""
		}],
		"wp:term": [{
			"taxonomy": "category",
			"embeddable": true,
			"href": ""
		}, {
			"taxonomy": "post_tag",
			"embeddable": true,
			"href": ""
		}],
		"curies": [{
			"name": "wp",
			"href": "https:\/\/api.w.org\/{rel}",
			"templated": true
		}]
	}
}]

The problem you might come across is getting information from your post meta data. Thanks to the register_meta() function we're able to define which fields show up on a REST API response. But what if you're using a plugin that hasn't set this field on the post meta you want to return, then you need to be able to modify the response of the endpoint and add the post meta that you need to the response.

To add an additional field to the REST API you need to use the function register_rest_field

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

For example let's say we have a post meta field of a publicly facing URL, therefore we can have WordPress sit on a private server and use the REST API to serve content to a JavaScript frontend. We have a post meta field of web_url that we need to return on each query to the post object. Using the register_rest_field function we need to attach this to the post object, with a field name of 'web_url' and register a callback function of get_web_url_meta to get the data we need for the response.

register_rest_field( 'post',
            'web_url',
            array(
                'get_callback' => 'get_web_url_meta',
            )
        );

Within the get_web_url_meta function we'll need to query the post meta using get_post_meta and return the result of the function. The return value will then be used in the REST response under a node title of web_url.

/**
     * @param $object
     * @param $field_name
     * @param $request
     *
     * @return mixed
     */
    public function get_web_url_meta( $object, $field_name, $request )
    {
        return get_post_meta( $object[ 'id' ], $field_name, true );
    }

Now in the JSON response we'll have a new field of web_url that will have the contents of the post meta data.

Upgrade to access all content on Paulund

Members unlock all tutorials and snippets

Access to all downloadable content

Access to code examples before others

Sign Up Now

Already a member? Login here

Subscribe To Newsletter

Get weekly updates to your email