{"id":218947,"date":"2011-09-25T08:00:00","date_gmt":"2011-09-25T08:00:00","guid":{"rendered":"https:\/\/www.weixiaoduo.com\/plugins\/2024\/02\/22\/wpml-json-api\/"},"modified":"2012-12-13T03:52:00","modified_gmt":"2012-12-13T03:52:00","slug":"wpml-json-api","status":"publish","type":"post","link":"https:\/\/www.weixiaoduo.com\/plugins\/wpml-json-api\/","title":{"rendered":"WPML JSON API"},"content":{"rendered":"<p>This plugin filters the response content of requests made to the WordPress <a href=\"https:\/\/wordpress.org\/extend\/plugins\/json-api\" rel=\"ugc\">JSON-API<\/a> to include data for translations created through the <a href=\"http:\/\/wpml.org\" rel=\"nofollow ugc\">WPML Multilingual CMS<\/a> plugin. One may also request the translation of response objects to a supported target language.<\/p>\n<p><a href=\"http:\/\/www.lettersandlight.org\" rel=\"nofollow ugc\">The Office of Letters and Light<\/a> uses this plugin to integrate WordPress and <a href=\"http:\/\/wpml.org\" rel=\"nofollow ugc\">WPML<\/a> with its Rails-built <a href=\"http:\/\/www.nanowrimo.org\" rel=\"nofollow ugc\">National Novel Writing Month<\/a> event-site application. We use WordPress to manage all back-end site content and our <a href=\"http:\/\/github.com\/lettersandlight\/kiosk\" rel=\"nofollow ugc\">Kiosk Ruby gem<\/a> for consuming, caching, searching, and serving the content through Rails. This could not have been so easily accomplished without the well-built and extensible <a href=\"https:\/\/wordpress.org\/extend\/plugins\/json-api\" rel=\"ugc\">JSON-API<\/a> for which we are grateful. And without the <a href=\"http:\/\/wpml.org\" rel=\"nofollow ugc\">WPML<\/a> plugin, we would not have such an intuitive and capable CMS i18n interface.<\/p>\n<h3>Usage<\/h3>\n<p>Please be familiar with the<br \/>\n<a href=\"https:\/\/wordpress.org\/extend\/plugins\/json-api\" rel=\"ugc\">JSON-API<\/a>.<\/p>\n<ol>\n<li>Augmented response objects\n<ul>\n<li>1.1. Language<\/li>\n<li>1.2. Translations<\/li>\n<\/ul>\n<\/li>\n<li>Filtering response objects by language\n<ul>\n<li>2.1. Specifying the language<\/li>\n<li>2.2. Filtered results<\/li>\n<\/ul>\n<\/li>\n<li>Response object translation\n<ul>\n<li>3.1 Specifying the target language<\/li>\n<li>3.2 Translated results<\/li>\n<li>3.3 Translatable objects and properties<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>1. Augmented response objects<\/h3>\n<h3>1.1. Language<\/h3>\n<p>After enabling the WPML JSON-API plugin, response objects will contain the<br \/>\nlanguage in which the object content was written.<\/p>\n<p>Let\u2019s take a look at an example response to a request made to <code>http:\/\/wordpress.site.example\/api\/get_page\/?dev=1&id=127<\/code>.<\/p>\n<pre><code>{\n  \"status\": \"ok\",\n  \"page\": {\n    \"id\": 127,\n    \"type\": \"page\",\n    \"slug\": \"the-best-page\",\n    \"url\": \"http:\/\/wordpress.site.example\/the-best-page\/\",\n    \"status\": \"publish\",\n    \"title\": \"The Best Page\",\n    \"title_plain\": \"The Best Page\",\n    \"content\": \"<p>This is the best page ever created.<\/p>n<blockquote><p>Best damn page ever. \u2013 Dan<\/p><\/blockquote>n\",\n    \"excerpt\": \"This is the best page ever created. Best damn page ever. \u2013 Dan\",\n    \"date\": \"2011-09-22 18:48:09\",\n    \"modified\": \"2011-09-22 18:54:55\",\n    \"categories\": [],\n    \"tags\": [],\n    \"author\": {\n      \"id\": 4,\n      \"slug\": \"dzachary\",\n      \"name\": \"Daniel Duvall\",\n      \"first_name\": \"Daniel\",\n      \"last_name\": \"Duvall\",\n      \"nickname\": \"Daniel Duvall\",\n      \"url\": \"\",\n      \"description\": \"\"\n    },\n    \"comments\": [],\n    \"attachments\": [],\n    \"comment_count\": 0,\n    \"comment_status\": \"open\",\n    \"translations\": {\n      \"en\": {\n        \"post_title\": \"The Best Page\",\n        \"post_status\": \"publish\",\n        \"id\": 21,\n        \"language_code\": \"en\",\n        \"is_original\": true,\n        \"resource_id\": 127\n      },\n      \"es\": {\n        \"post_title\": \"La Pu00e1gina Mejor\",\n        \"post_status\": \"publish\",\n        \"id\": 22,\n        \"language_code\": \"es\",\n        \"is_original\": false,\n        \"resource_id\": 131\n      }\n    },\n    \"language\": \"en\"\n  }\n}\n<\/code><\/pre>\n<p>The <code>language<\/code> property specifies the code of the language in which the page<br \/>\nwas written\u2014pretty simple.<\/p>\n<h3>1.2. Translations<\/h3>\n<p>The <code>translations<\/code> objects of the same example response given in Section 1.1.<br \/>\ninclude data for each <em>published<\/em> resource that comprises a translation of<br \/>\nits parent object.<\/p>\n<pre><code>\"translations\": {\n  \"en\": {\n    \"post_title\": \"The Best Page\",\n    \"post_status\": \"publish\",\n    \"id\": 21,\n    \"language_code\": \"en\",\n    \"is_original\": true,\n    \"resource_id\": 127\n  },\n  \"es\": {\n    \"post_title\": \"La Pu00e1gina Mejor\",\n    \"post_status\": \"publish\",\n    \"id\": 22,\n    \"language_code\": \"es\",\n    \"is_original\": false,\n    \"resource_id\": 131\n  }\n<\/code><\/pre>\n<p>Properties included in each member of the <code>translations<\/code> object depend on the<br \/>\ntype of parent object.<\/p>\n<p>All <code>post<\/code> and <code>page<\/code> objects will include the following.<\/p>\n<ul>\n<li><code>id<\/code> \u2013 The ID of the translation object.<\/li>\n<li><code>language_code<\/code> \u2013 The code of the language in which the translation is written. Each translation object is also keyed by this value.<\/li>\n<li><code>is_original<\/code> \u2013 Specifies whether the language of this translation is the language in which the original work was written.<\/li>\n<li><code>resource_id<\/code> \u2013 The ID of the translated version of the parent object.<\/li>\n<li><code>post_title<\/code> \u2013 The title of the translated post\/page.<\/li>\n<li><code>post_status<\/code> \u2013 The status of the translated post\/page. Always \u2018publish\u2019.<\/li>\n<li><code>post_id<\/code> \u2013 Redundancy of <code>resource_id<\/code>.<\/li>\n<\/ul>\n<p>All <code>category<\/code> objects will include the following.<\/p>\n<ul>\n<li><code>id<\/code> \u2013 The ID of the translation object.<\/li>\n<li><code>language_code<\/code> \u2013 The code of the language to which the category was translated.<\/li>\n<li><code>is_original<\/code> \u2013 Specifies whether the language of this translation is the language of the original category.<\/li>\n<li><code>resource_id<\/code> \u2013 The ID of the translated version of the parent object.<\/li>\n<li><code>name<\/code> \u2013 The translated category name.<\/li>\n<li><code>term_id<\/code> \u2013 Redundancy of <code>resource_id<\/code>.<\/li>\n<li><code>post_count<\/code> \u2013 The number of posts tagged with the translated category.<\/li>\n<\/ul>\n<h3>2. Filtering response objects by language<\/h3>\n<p>You may tell the API to only return objects for a specific language by using<br \/>\nthe <code>language<\/code> parameter.<\/p>\n<h3>2.1. Specifying the language<\/h3>\n<p>The <code>language<\/code> parameter should be one of the supported language codes.<\/p>\n<p>Example:<\/p>\n<ul>\n<li><code>http:\/\/wordpress.site.example\/api\/get_category_index\/?language=es&dev=1<\/code><\/li>\n<\/ul>\n<h3>2.2. Filtered results<\/h3>\n<p>Following is a example response to the example request in Section 2.1.<\/p>\n<pre><code>{\n  \"status\": \"ok\",\n  \"count\": 2,\n  \"categories\": [\n    {\n      \"id\": 13,\n      \"slug\": \"noticias-de-ultima-hora-2\",\n      \"title\": \"Noticias de u00daltima Hora\",\n      \"description\": \"\",\n      \"parent\": 0,\n      \"post_count\": 1,\n      \"translations\": {\n        \"en\": {\n          \"name\": \"Breaking News\",\n          \"term_id\": 4,\n          \"post_count\": 2,\n          \"id\": 9,\n          \"language_code\": \"en\",\n          \"is_original\": true,\n          \"resource_id\": 4\n        },\n        \"es\": {\n          \"name\": \"Noticias de u00daltima Hora\",\n          \"term_id\": 13,\n          \"post_count\": 1,\n          \"id\": 26,\n          \"language_code\": \"es\",\n          \"is_original\": false,\n          \"resource_id\": 13\n        }\n      },\n      \"language\": \"es\"\n    }\n  ]\n}<h3>3. Response object translation<\/h3>\n<\/code><\/pre>\n<p>You can request objects to be translated before they are returned. This may<br \/>\nsave you some requests if you have the slug or ID and just need the object in<br \/>\na specific language.<\/p>\n<h3>3.1. Specifying the target language<\/h3>\n<p>The target language can be specified by passing a <code>to_language<\/code> parameter. The<br \/>\nvalue should be one of the supported language codes.<\/p>\n<p>Example:<\/p>\n<ul>\n<li><code>http:\/\/wordpress.site.example\/api\/get_page\/?slug=the-best-page&to_language=es&dev=1<\/code><\/li>\n<\/ul>\n<h3>3.2. Translated results<\/h3>\n<p>Following is an example response to the example request in Section 3.1.<\/p>\n<pre><code>{\n  \"status\": \"ok\",\n  \"page\": {\n    \"id\": 127,\n    \"type\": \"page\",\n    \"slug\": \"la-pagina-mejor\",\n    \"url\": \"http:\/\/wordpress.site.example\/the-best-page\/\",\n    \"status\": \"publish\",\n    \"title\": \"La Pu00e1gina Mejor\",\n    \"title_plain\": \"La Pu00e1gina Mejor\",\n    \"content\": \"<p>Esta es la pu00e1gina mejor se haya creado.<\/p>n<blockquote><p>Best damn page ever. \u2013 Dan<\/p><\/blockquote>n\",\n    \"excerpt\": \"Esta es la pu00e1gina mejor se haya creado. Best damn page ever. \u2013 Dan\",\n    \"date\": \"2011-09-22 18:48:09\",\n    \"modified\": \"2011-09-22 18:54:55\",\n    \"categories\": [],\n    \"tags\": [],\n    \"author\": {\n      \"id\": 4,\n      \"slug\": \"dzachary\",\n      \"name\": \"Daniel Duvall\",\n      \"first_name\": \"Daniel\",\n      \"last_name\": \"Duvall\",\n      \"nickname\": \"Daniel Duvall\",\n      \"url\": \"\",\n      \"description\": \"\"\n    },\n    \"comments\": [],\n    \"attachments\": [],\n    \"comment_count\": 0,\n    \"comment_status\": \"open\",\n    \"translations\": {\n      \"en\": {\n        \"post_id\": 127,\n        \"post_title\": \"The Best Page\",\n        \"post_status\": \"publish\",\n        \"id\": 21,\n        \"language_code\": \"en\",\n        \"is_original\": true,\n        \"resource_id\": 127\n      },\n      \"es\": {\n        \"post_id\": 131,\n        \"post_title\": \"La Pu00e1gina Mejor\",\n        \"post_status\": \"publish\",\n        \"id\": 22,\n        \"language_code\": \"es\",\n        \"is_original\": false,\n        \"resource_id\": 131\n      }\n    },\n    \"language\": \"es\",\n    \"translated\": true,\n    \"original_slug\": \"the-best-page\",\n    \"original_title\": \"The Best Page\",\n    \"original_title_plain\": \"The Best Page\"\n  }\n}\n<\/code><\/pre>\n<p>As you can see the <code>slug<\/code>, <code>title<\/code>, <code>title_plain<\/code>, <code>excerpt<\/code>, and <code>content<\/code><br \/>\nproperties have been rewritten with the Spanish translation. In addition, the<br \/>\noriginal properties have been prefixed with <code>original_<\/code>. This is always the<br \/>\ncase for all properties except for <code>excerpt<\/code> and <code>content<\/code>. In an effort to<br \/>\nsave on response size, the latter have been omitted.<\/p>\n<h3>3.3. Translatable objects and properties<\/h3>\n<p>Here are all of the translatable object types and their translatable<br \/>\nproperties, those that can be rewritten as described in Section 3.2.<\/p>\n<p>Objects <code>post<\/code> and <code>page<\/code>:<\/p>\n<ul>\n<li><code>slug<\/code><\/li>\n<li><code>title<\/code><\/li>\n<li><code>title_plain<\/code><\/li>\n<li><code>excerpt<\/code><\/li>\n<li><code>content<\/code><\/li>\n<\/ul>\n<p>Objects <code>tag<\/code> and <code>category<\/code>:<\/p>\n<ul>\n<li><code>slug<\/code><\/li>\n<li><code>title<\/code><\/li>\n<li><code>description<\/code><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>An extension to JSON-API for sites using the WPML Multilingual CMS plugin.<\/p>\n","protected":false},"author":65,"featured_media":351962,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"WPML JSON API - \u8587\u6653\u6735\u63d2\u4ef6\u76ee\u5f55","description":"An extension to JSON-API for sites using the WPML Multilingual CMS plugin."},"footnotes":""},"categories":[1],"tags":[461,977,2526,498,19],"class_list":["post-218947","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-api","tag-cms","tag-i18n","tag-json","tag-wpml"],"_links":{"self":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts\/218947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/comments?post=218947"}],"version-history":[{"count":0,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts\/218947\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/media\/351962"}],"wp:attachment":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/media?parent=218947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/categories?post=218947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/tags?post=218947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}