[WordPress] 外掛分享: Better REST API Featured Images

首頁外掛目錄 › Better REST API Featured Images
3,000+
安裝啟用
★★★★★
5/5 分(11 則評價)
3517 天前
最後更新
問題解決
WordPress 4.0+ v1.2.1 上架:2015-10-12

內容簡介

注意:您可能不需要此外掛。 REST API 已經支援在 URL 中添加查詢參數 ?_embed,並且回應將包括所有“嵌入式媒體”,包括特色圖片,在那裡獲得的數據正是此外掛提供的數據。現在使用此外掛的唯一原因是,如果您喜歡在回應中將精選圖片數據放在頂層欄位而不是在嵌入式媒體中的 _embedded 欄位中,以及如果您始終需要在回應中獲取特色圖片數據,而不必使用 ?_embed。我仍然使用此外掛,因為我通常都需要這兩個功能,但使用此外掛之前請務必嘗試 ?_embed。🙂

WordPress REST API 的第 2 版默認返回帖子對象上的 featured_media 欄位(以前是 featured_image),但是此欄位只是圖像ID。

此外掛向帖子對象添加了一個 better_featured_image 欄位,其中包含可用的圖片大小和 URL,可以讓您在不進行第二個請求的情況下獲取此信息。

它將此:

"featured_media": 13,

變成了這樣:

"featured_media": 13,
"better_featured_image": {
"id": 13,
"alt_text": "熱氣球",
"caption": "該活動提供熱氣球騎行",
"description": "該大型活動的熱氣球",
"media_type": "image",
"media_details": {
"width": 5760,
"height": 3840,
"file": "2015/09/balloons.jpg",
"sizes": {
"thumbnail": {
"file": "balloons-150x150.jpg",
"width": 150,
"height": 150,
"mime-type": "image/jpeg",
"source_url": "http://api.example.com/wp-content/uploads/2015/09/balloons-150x150.jpg"
},
"medium": {
"file": "balloons-300x200.jpg",
"width": 300,
"height": 200,
"mime-type": "image/jpeg",
"source_url": "http://api.example.com/wp-content/uploads/2015/09/balloons-300x200.jpg"
},
"large": {
"file": "balloons-1024x683.jpg",
"width": 1024,
"height": 683,
"mime-type": "image/jpeg",
"source_url": "http://api.example.com/wp-content/uploads/2015/09/balloons-1024x683.jpg"
},
"post-thumbnail": {
"file": "balloons-825x510.jpg",
"width": 825,
"height": 510,
"mime-type": "image/jpeg",
"source_url": "http://api.example.com/wp-content/uploads/2015/09/balloons-825x510.jpg"
}
},
"image_meta": {
"aperture": 6.3,
"credit": "",
"camera": "Canon EOS 5D Mark III",
"caption": "",
"created_timestamp": 1433110262,
"copyright": "",
"focal_length": "50",
"iso": "100",
"shutter_speed": "0.004",
"title": "",
"orientation": 1
}
},
"post": null,
"source_url": "http://api.example.com/wp-content/uploads/2015/09/balloons.jpg"
},

回應的格式與發送請求到 /wp-json/wp/v2/media/13 或使用 ?_embed 獲取的格式幾乎相同。當未在帖子上設置特色圖片時,better_featured_image 欄位的值將為 null。

我已進行了一些基本的性能測試,表明使用此外掛和不使用的回應時間差異約為

外掛標籤

開發者團隊

⬇ 下載最新版 (v1.2.1) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「Better REST API Featured Images」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

Note: You probably do not need this plugin. The REST API already supports adding the query param ?_embed to your URL and the response will then include all “embedded media”, including the featured image, and the data you get there is exactly what this plugin gives you. The only reasons to use this plugin at this point are if you prefer to have the featured image data in a top level field in the response rather than among other embedded media in the _embedded field, and if you always want the featured image data in the response rather than having to ask for it with ?_embed. I still use this plugin because I do usually want both these things, but definitely give ?_embed a try before using this plugin. 🙂
Version 2 of the WordPress REST API returns a featured_media field (formerly featured_image) on the post object by default, but this field is simply the image ID.
This plugin adds a better_featured_image field to the post object that contains the available image sizes and urls, allowing you to get this information without making a second request.
It takes this:
"featured_media": 13,

And turns it into this:
"featured_media": 13,
"better_featured_image": {
"id": 13,
"alt_text": "Hot Air Balloons",
"caption": "The event featured hot air balloon rides",
"description": "The hot air balloons from the big event",
"media_type": "image",
"media_details": {
"width": 5760,
"height": 3840,
"file": "2015/09/balloons.jpg",
"sizes": {
"thumbnail": {
"file": "balloons-150x150.jpg",
"width": 150,
"height": 150,
"mime-type": "image/jpeg",
"source_url": "http://api.example.com/wp-content/uploads/2015/09/balloons-150x150.jpg"
},
"medium": {
"file": "balloons-300x200.jpg",
"width": 300,
"height": 200,
"mime-type": "image/jpeg",
"source_url": "http://api.example.com/wp-content/uploads/2015/09/balloons-300x200.jpg"
},
"large": {
"file": "balloons-1024x683.jpg",
"width": 1024,
"height": 683,
"mime-type": "image/jpeg",
"source_url": "http://api.example.com/wp-content/uploads/2015/09/balloons-1024x683.jpg"
},
"post-thumbnail": {
"file": "balloons-825x510.jpg",
"width": 825,
"height": 510,
"mime-type": "image/jpeg",
"source_url": "http://api.example.com/wp-content/uploads/2015/09/balloons-825x510.jpg"
}
},
"image_meta": {
"aperture": 6.3,
"credit": "",
"camera": "Canon EOS 5D Mark III",
"caption": "",
"created_timestamp": 1433110262,
"copyright": "",
"focal_length": "50",
"iso": "100",
"shutter_speed": "0.004",
"title": "",
"orientation": 1
}
},
"post": null,
"source_url": "http://api.example.com/wp-content/uploads/2015/09/balloons.jpg"
},

The format of the response is nearly identical to what you would get sending a request to /wp-json/wp/v2/media/13 or using ?_embed. When no featured image has been set on the post the better_featured_image field will have a value of null.
I’ve done some basic performance tests that indicate the difference in response times with and without this plugin to be about 10-15ms for a collection of 10 posts and 0-5ms for a single post. For me this is much faster than making a second request to /media/, especially for multiple posts.
As of version 1.1.0, there is a filter better_rest_api_featured_image that allows you to add custom data to the better_featured_image field. The filter is directly on the return value of the function that returns the better_featured_image field. This can be used to do things like add custom image meta or an SVG version of the image to the response. Here’s an example of how you might use it:
add_filter( 'better_rest_api_featured_image', 'xxx_modify_rest_api_featured_image', 10, 2 );
/**
* Modify the Better REST API Featured Image response.
*
* @param array $featured_image The array of image data.
* @param int $image_id The image ID.
*
* @return array The modified image data.
*/
function xxx_modify_rest_api_featured_image( $featured_image, $image_id ) {

// Add an extra_data_string field with a string value.
$featured_image['extra_data_string'] = 'A custom value.';

// Add an extra_data_array field with an array value.
$featured_image['extra_data_array'] = array(
'custom_key' => 'A custom value.',
);

return $featured_image;
}

This plugin is on on Github and pull requests are always welcome. 🙂

延伸相關外掛

文章
Filter
Apply Filters
Mastodon