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%
Media Library Assistant
The Media Library Assistant provides several enhancements for managing the Media Library, including: Complete support for ALL taxonomies, including the standard Categories and Tags, your custom taxonomies and the Assistant’s pre-defined Att. Categories and Att. Tags. You can add taxonomy columns to the Media/Assistant listing, filter on any taxonomy, assign terms and list the attachments for a term. The Media/Assistant admin screen displays more attachment information such as parent information, file URL and image metadata. Provides many more listing columns (more than 20) to choose from. You can also add columns to display custom field values. Provides additional view filters for MIME types and taxonomies, and features to compose custom views of your own. Inline “Bulk Edit” and “Quick Edit” areas; update author, parent and custom fields, add, remove or replace taxonomy terms for several attachments at once. Works on the Media/Add New screen as well. Works with the popular Admin Columns plugin for even more Media/Assistant screen customization. IPTC, EXIF (including GPS), XMP and PDF metadata can be assigned to standard WordPress fields, taxonomy terms and custom fields. You can update all existing attachments from the Settings page IPTC/EXIF tab, groups of existing attachments with a Bulk Action or one existing attachment from the Edit Media/Edit Single Item screen. Twelve hooks provided for complete mapping customization from your theme or plugin code. You can view and/or download this PDF document with more information: Mapping File Metadata to WordPress Fields with Media Library Assistant Attachment metadata such as file size, image dimensions and where-used information can be assigned to WordPress custom fields. You can then use the custom fields in your [mla_gallery] display and you can add custom fields as sortable, searchable columns in the Media/Assistant submenu table. You can also modify the WordPress _wp_attachment_metadata contents to suit your needs. Complete control over Post MIME Types, File Upload extensions/MIME Types and file type icon images. Fifty four (54) additional upload types, 112 file type icon images and a searchable list of over 1,500 file extension/MIME type associations. Complete control over Intermediate Image Sizes, used by WordPress to generate and access intermediate image sizes for Media Library items. The [mla_gallery] shortcode, used in a post, page or custom post type to add a gallery of images and/or other Media Library items (such as PDF documents). MLA Gallery is a superset of the WordPress [gallery] shortcode; it is compatible with [gallery] and provides many enhancements. These include: 1) full query and display support for WordPress categories, tags, custom taxonomies and custom fields, 2) support for all post_mime_type values, not just images 3) media Library items need not be “attached” to the post, and 4) control over the styles, markup and content of each gallery using Style and Markup Templates. Twenty-eight hooks are provided for complete gallery customization from your theme or plugin code. The [mla_tag_cloud] shortcode, displays a “cloud” of the “most used” terms in your Media Library where the size of each term is determined by how many times that particular term has been assigned to Media Library items. Twenty-five hooks are provided for complete cloud customization from your theme or plugin code. The [mla_term_list] shortcode, displays hierarchical (and flat) taxonomy terms in list, dropdown control or checklist formats. Twenty hooks are provided for complete list customization from your theme or plugin code. The [mla_custom_list] shortcode, displays flat/cloud lists, dropdown controls and checkbox lists of custom field values. Twenty-seven hooks are provided for complete list customization from your theme or plugin code. The [mla_archive_list] shortcode, displays flat/cloud lists and dropdown controls of date-based values. Twenty hooks are provided for complete list customization from your theme or plugin code. Powerful Content Templates, which let you compose a value from multiple data sources, mix literal text with data values, test for empty values and choose among two or more alternatives or suppress output entirely. Support for WPML and Polylang multi-language CMS plugins. MLA has earned a place on WPML’s List of Recommended Plugins. Works with Photonic Gallery, Justified Image Grid, Jetpack and other plugins, so you can add slideshows, thumbnail strips and special effects to your [mla_gallery] galleries. Works with WordPress Real Media Library: Media Library Folder & File Manager (Lite and Pro) to organize your files into folders, collections and galleries. This combination enhances both the Media/Assistant admin submenu and the [mla_gallery] shortcode. Works with CatFolders – WP Media Folders (Lite and Pro) to categorize media files better and faster. This combination enhances both the Media/Assistant admin submenu and the [mla_gallery] shortcode. Enhanced Search Media box. Search can be extended to the name/slug, ALT text and caption fields. The connector between search terms can be “and” or “or”. Search by attachment ID or Parent ID is supported, and you can search on keywords in the taxonomy terms assigned to Media Library items. Works in the Media Manager Modal Window, too. Taxonomy and custom field support in the ATTACHMENT DETAILS pane of the Media Manager Modal Window and Media/Library Grid view. The Assistant is designed to work like the standard Media Library pages, so the learning curve is short and gentle. Contextual help is provided on every new screen to highlight new features. NOTE: Complete documentation is included in the Documentation tab on the Settings/Media Library Assistant admin screen and the drop-down “Help” content in the admin screens. You can find a stand-alone version of the Documentation on my web site: Media Library Assistant Documentation I do not solicit nor accept personal donations in support of the plugin. WordPress and its global community means a lot to me and I am happy to give something back. If you find the Media Library Assistant plugin useful and would like to support a great cause, consider a tax-deductible donation to our Chateau Seaview Fund at the ALS Network. Every dollar of the fund goes to make the lives of people with ALS, their families and caregivers easier. Thank you! Acknowledgements Media Library Assistant includes many images drawn (with permission) from the Crystal Project Icons, created by Everaldo Coelho, founder of Yellowicon. Many thanks to Aurovrata Venet, Il’ya Karastel and Kristian Adolfsson for testing and advising on the multilingual support features! The Example Plugins The MLA example plugins have been developed to illustrate practical applications that use the hooks MLA provides to enhance the admin-mode screens and front-end content produced by the MLA shortcodes. Most of the examples are drawn from topics in the MLA Support Forum. The Documentation/Example Plugins submenu lets you browse the list of MLA example plugins, install or update them in the Plugins/Installed Plugins area and see which examples you have already installed. To activate, deactivate or delete the plugins you must go to the Plugins/Installed Plugins admin submenu. The Example plugins submenu lists all of the MLA example plugins and identifies those already in the Installed Plugins area. In the submenu: the “Screen Options” dropdown area lets you choose which columns to display and how many items appear on each page the “Help” dropdown area gives you a brief explanation of the submenu content and functions the “Search Plugins” text box lets you filter the display to items containing one or more keywords or phrases bulk and rollover actions are provided to install or update example plugins the table can be sorted by any of the displayed columns Once you have installed an example plugin you can use the WordPress Plugins/Editor submenu to view the source code and (with extreme caution) make small changes to the code. Be very careful if you choose to modify the code! Making changes to active plugins is not recommended. If your changes cause a fatal error, the plugin will be automatically deactivated. It is much safer to download the file(s) or use FTP access to your site to modify the code offline in a more robust HTML/PHP editor. You can use the “Download” rollover action to download a plugin to your local system. Once you have made your modifications you can copy the plugin to a compressed file (ZIP archive) and then upload it to your server with the Plugins/Add New (Upload Plugin) admin submenu. If you do make changes to the example plugin code the best practice is to save the modified file(s) under a different name, so your changes won’t be lost in a future update. If you want to retain the file name, consider changing the version number, e.g. adding 100 to the MLA value, so you can more easily identify the plugins you have modified.