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%
Custom Query Shortcode
This plugin gives you [query] shortcode which enables you to query and output any posts filtered by specific attributes. Usage You can use most parameters supported by WP_Query class to filter the posts; you can query for specific post types, categories, tags, authors, etc. Other supported parameters Aside from WP_Query parameters, the shortcode also supports the following additional parameters: featured: to query for sticky posts which by default are excluded from the query. thumbnail_size: to specify the size of the {THUMBNAIL} images. You can use built-in image sizes or custom ones you’ve defined. content_limit: to limit the number of words of the {CONTENT} var; by default it’s “0” which means it outputs the whole content. posts_separator: text to display between individual posts. lens: custom output template – see description below. twig_template: output template using Twig templating engine – requires the Timber library. Formatting the output You can define how you want to format the output inline within an opening [query] and closing [/query] tag. Available keywords are: TITLE, CONTENT, AUTHOR, AUTHOR_URL, DATE, THUMBNAIL, CONTENT, COMMENT_COUNT. The following example will display the latest 5 posts from the category with the ID of 3, showing a post title and comment count, with a link to the post: [query posts_per_page=”5″ cat=”3″] {TITLE} ({COMMENT_COUNT}) [/query] Grid display With the “cols” parameter you can display the output in a grid. [query posts_per_page=”3″ cols=”3″] {THUMBNAIL} {TITLE} {CONTENT} [/query] will display the latest 3 posts in the defined template, in 3 columns. The plugin will automatically divide the grid into rows based upon the ‘posts_per_page’ option, divided by the ‘cols’ option. Lenses (output templates) With the “lens” parameter you can customize the display of the query results using a template. Some basic lenses/templates are provided: ul: unordered list of linked post titles. ul-title-date: same as ‘ul’, but also displays the posted date. article-excerpt: series of articles, with a header containing the linked post title, and the excerpt. article-excerpt-date: same as ‘article-excerpt’, but also displays the posted date. cards: displays the post thumb above the header with linked post title, followed by the excerpt. Bootstrap lenses Some pre-defined lenses/templates are provided which use JavaScript Components from the Bootstrap CSS framework. The generated markup is compliant with the 5.x version of Bootstrap. This feature relies on Bootstrap library to be already loaded on the page, the plugin does not include it. If you’re using a Bootstrap-based theme, this should work; otherwise you can use the Bootstrap plugin). Tabs This will show the latest 3 posts in a tabbed widget. [query posts_per_page=”3″ lens=”tabs”] Accordion This will create an accordion widget of all our posts from the “faq” post type. [query posts_per_page=”0″ post_type=”faq” lens=”accordion”] Carousel This creates a carousel of latest five featured posts: [query posts_per_page=”5″ featured=”true” lens=”carousel”] Custom Lenses/templates You can create your own custom templates and put them into one of these pre-defined folder names within your theme: ‘query-shortcode-templates’ ‘partials/query-shortcode-lenses/’ ‘html/lenses/’ Or simply specify your own subfolder in the ‘lens’ parameter: [query lens=”folder/template-name”] Twig Template Support Starting with version 0.4, you can use Twig templates for your output. Support for Twig is provided by the Timber library. This requires that Timber 2.x be installed as a Composer dependency. To use a Twig template for your query output, simply use the 'twig_template' parameter instead of the 'lens' parameter, and provide the path to your template. By default, Timber looks within the views folder in your active theme. You can change the default template location in Timber. Examples: [query twig_template="template-name.twig"] [query twig_template="folder/template-name.twig"]