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. 🙂
Top keywords
- image33×4.94%
- featured24×3.59%
- featured image18×2.69%
- api12×1.80%
- data10×1.50%
- field10×1.50%
- jpg10×1.50%
- media9×1.35%
- better7×1.05%
- response7×1.05%
- rest7×1.05%
- rest api7×1.05%
Flexible Recent Posts
Looking for maintainer. Write me to
[email protected] Flexible Recent Posts (FRP) plugin gives you possibility to add widget to your blog with recent posts using user-defined template. The main features are: Template for each post in widget. You can write your HTML-template or overwrite default in widget form. Shortcodes support in template. You can use built-in shortcodes to insert info related to current post in widget: title, featured image, excerpt, date, permalink, author, comments. Restrict terms from which you want to display posts. Display posts from all terms or from selected only. Number of posts to display. Set number of latest posts that will be displayed in widget. All posts links. Select option to display link near the title or/and after all posts that will open selected posts category page. Set link title or custom link URL. Useful links: Template shortcodes Feature requests Bugs Donate (Flattr) Translators: Russian (ru) – Paul Annekov Licenses Uses FatCow Icon Set licensed under Creative Commons Attribution 3.0 License. Shortcodes Plugin template system supports 5 shortcodes related to current widget post, that you can use in your template: Title [frp_title] Displays post title. Example: [frp_title] Excerpt [frp_excerpt length=””] Displays post excerpt using get_the_excerpt function. You can specify excerpt length using length attribute. If not specified – default WordPress excerpt length used. Example: [frp_excerpt] , [frp_excerpt length="15"] Date [frp_date format=”F j, Y” time_since=”0″] Displays post date. You can add format shortcode parameter to change date format. Read about format value here. Add time_since parameter with 1 as value to display amount of elapsed time: 30 seconds ago, 5 minutes ago, 12 hours ago, 3 Jun. Example: [frp_date format="Y/m/d g:i:s A"] , [frp_date time_since="1"] Link [frp_link] Displays post permalink. Example: read more Featured image [frp_thumbnail size=”32×32″] Displays featured post image. Add size shortcode parameter to change image size. You can define size using WIDTHxHEIGHT as value or size name (built-in or defined in theme). Examples: [frp_thumbnail], [frp_thumbnail size="thumbnail"], [frp_thumbnail size="128x128"], [frp_thumbnail size="full"] Author [frp_author link=”1″] Displays name of the post’s author. Set link parameter to 0 if you want to disable link (author’s site) from author name. Examples: [frp_author], [frp_author link="1"], [frp_author link="0"] Comments [frp_comments no_text=”1″] Displays the number of comments. Set no_text parameter to 1 if you want to disable comment text (just “23” instead of “23 Comments”). Examples: [frp_comments], [frp_comments no_text="1"], [frp_comments no_text="0"]
Top keywords