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%
WebP Express
More than 9 out of 10 users are using a browser that is able to display webp images. Yet, on most websites, they are served jpeg images, which are typically double the size of webp images for a given quality. What a waste of bandwidth! This plugin was created to help remedy that situation. With little effort, WordPress admins can have their site serving autogenerated webp images to browsers that supports it, while still serving jpeg and png files to browsers that does not support webp. The image converter The plugin uses the WebP Convert library to convert images to webp. WebP Convert is able to convert images using multiple methods. There are the “local” conversion methods: imagick, cwebp, vips, gd. If none of these works on your host, there are the cloud alternatives: ewww (paid) or connecting to a WordPress site where you got WebP Express installed and you enabled the “web service” functionality. The “Serving webp to browsers that supports it” part. The plugin supports different ways of delivering webps to browsers that supports it: By routing jpeg/png images to the corresponding webp – or to the image converter if the image hasn’t been converted yet. By altering the HTML, replacing image tags with picture tags. Missing webps are auto generated upon visit. By altering the HTML, replacing image URLs so all points to webp. The replacements only being made for browsers that supports webp. Again, missing webps are auto generated upon visit. In combination with Cache Enabler, the same as above can be achieved, but with page caching. You can also deliver webp to all browsers and add the webpjs javascript, which provides webp support for browsers that doesn’t support webp natively. However, beware that the javascript doesn’t support srcset attributes, which is why I haven’t added that method to the plugin (yet). The plugin implements the “WebP On Demand” solution described here and builds on a bunch of open source libraries (all maintained by me): – WebP Convert: For converting images to webp – WebP Convert Cloud Service: For the Web Service functionality – DOM Util for WebP: For the Alter HTML functionality – Image MimeType Guesser: For detecting mime types of images. – HTAccess Capability Tester: For testing .htaccess capabilities in a given directory, using live tests – WebP Convert File Manager: For browsing conversions and triggering conversions. – Exec With Fallback: For emulating exec() on systems where it is disabled (using proc_open(), passthru() or similar alternatives). Benefits Much faster load time for images in browsers that supports webp. The converted images are typically less than half the size (for jpeg), while maintaining the same quality. Bear in mind that for most web sites, images are responsible for the largest part of the waiting time. Better user experience (whether performance goes from terrible to bad, or from good to impressive, it is a benefit). Better ranking in Google searches (performance is taken into account by Google). Less bandwidth consumption – makes a huge difference in the parts of the world where the internet is slow and costly (you know, ~80% of the world population lives under these circumstances). Currently ~97% of all traffic are done with browsers supporting webp. It’s great for the environment too! Reducing network traffic reduces electricity consumption which reduces CO2 emissions. Limitations The plugin should now work on Microsoft IIS server, but it has not been tested thoroughly. Supporting WebP Express Bread on the table don’t come for free, even though this plugin does, and always will. I enjoy developing this, and supporting you guys, but I kind of need the bread too. Please make it possible for me to continue wasting time on this plugin: Buy me a Coffee Buy me coffee on a regular basis and help ensuring my coffee supplies doesn’t run dry. Supporters of WebP Express Persons who recently contributed with ko-fi – Thanks! 5 Jan: Joel 24 Dec: Patrick Müller 16 Dec: Dragos 9 Aug: Tanzi 3 Jul: Jen 26 Jun: Per 16 May: Erick Danzer 8 May: Mike 31 May: parallactic 14 May: Gitte Rebsdorf 9 May: La Braud Persons who recently contributed on github sponsors – Thanks! * 16 Dec: kcrlost * 16 Dec: Yakovos Frountas (Greece) Persons who contributed with extra generously amounts of coffee / lifetime backing (>80$) – thanks!: Patrick Müller ($250) Max Kreminsky ($115) Justin – BigScoots ($105) Bill Vallance ($102) Joel ($100) Label Vier ($100) Sebastian ($99) Tammy Lee ($90)