Chris Townsend Chris Townsend - 7 months ago 10
PHP Question

How to reference an object that has a semi colon in

I'm using AngularJS with the WordPress Rest API. I am making a get request which returns an object. To get a featured image I have to use the '?embed' parameter which gives be another object called _embedded.

The problem is the object I want that is inside _embedded is called wp:featuredmedia. If I reference it like this in angular I get a syntax error.

Here is my code

$http.get(queries[0], {'cache': true}).
then(function(response) {
$scope.careers_title = strip(response.data.title.rendered);
$scope.careers_content = strip(response.data.content.rendered);
$scope.careers_feature_image = strip(response.data.featured_media);
console.log(response.data._embedded);
});


The console.log returns this

Object {author: Array[1], wp:featuredmedia: Array[1], wp:term: Array[2]}author: Array[1]wp:featuredmedia: Array[1]0: Object_links: Objectalt_text: ""author: 1date: "2016-04-25T09:33:52"id: 46link: "http://localhost:8888/rubis/wordpress/energy-efficiency/tp-roundall/"media_details: Objectmedia_type: "image"mime_type: "image/png"slug: "tp-roundall"source_url: "http://localhost:8888/rubis/wordpress/wp-content/uploads/2016/04/tp-roundall.png"title: Objecttype: "attachment"__proto__: Objectlength: 1__proto__: Array[0]wp:term: Array[2]__proto__: Object

Answer

Indeed you cannot write:

response.data._embedded.wp:featuredmedia

This is a forbidden property name. However any string is accepted so you can access it this way:

response.data._embedded['wp:featuredmedia']