Title: Better REST API Featured Images
Author: Braad
Published: <strong>13 de octubre de  2015</strong>
Last modified: 30 de julio de  2016

---

Buscar plugins

Este plugin **no se ha probado con las últimas 3 versiones mayores de WordPress**.
Puede que ya no tenga asistencia ni lo mantenga nadie, o puede que tenga problemas
de compatibilidad cuando se usa con las versiones más recientes de WordPress.

![](https://s.w.org/plugins/geopattern-icon/better-rest-api-featured-images.svg)

# Better REST API Featured Images

 Por [Braad](https://profiles.wordpress.org/braad/)

[Descargar](https://downloads.wordpress.org/plugin/better-rest-api-featured-images.1.2.1.zip)

 * [Detalles](https://es-ar.wordpress.org/plugins/better-rest-api-featured-images/#description)
 * [Valoraciones](https://es-ar.wordpress.org/plugins/better-rest-api-featured-images/#reviews)
 *  [Instalación](https://es-ar.wordpress.org/plugins/better-rest-api-featured-images/#installation)
 * [Desarrollo](https://es-ar.wordpress.org/plugins/better-rest-api-featured-images/#developers)

 [Soporte](https://wordpress.org/support/plugin/better-rest-api-featured-images/)

## Descripción

**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](https://github.com/BraadMartin/better-rest-api-featured-images)
and pull requests are always welcome. 🙂

## Instalación

#### Manual Installation

 1. Upload the entire `/better-rest-api-featured-images` directory to the `/wp-content/
    plugins/` directory.
 2. Activate ‘Better REST API Featured Images’ through the ‘Plugins’ menu in WordPress.

#### Better Installation

 1. Go to Plugins > Add New in your WordPress admin and search for ‘Better REST API
    Featured Images’.
 2. Click Install.

## Preguntas frecuentes

  How does it work?

The WP REST API includes a filter on the response data it returns, and this plugin
uses that filter to add a new field `better_featured_image` with the extra data 
for the featured image.

  When does the plugin load?

The plugin loads on `init` at priority 12, in order to come after any custom post
types have been registered.

  Why doesn’t the plugin replace the default `featured_media` field?

The `featured_media` field is a core field, and other applications might expect 
it to always be an integer value. To avoid any issues, this plugin includes the 
extra data under the `better_featured_image` field name.

  Why is the core field called `featured_media` but the plugin field is `better_featured_image`?

Prior to V2 Beta 11 of the REST API the core field was called `featured_image`. 
As of Beta 11 this field was changed to `featured_media`, with the idea that at 
some point in the future there may be additional media items included on this field
beyond the featured image. Version 1.1.1 of this plugin is compatible with both 
Beta 11 and all previous versions of V2.

## Reseñas

![](https://secure.gravatar.com/avatar/b9b2c306defb378081df249f209fba25511f60377e7f4e0c03745b9584632c6b?
s=60&d=retro&r=g)

### 󠀁[Awesome](https://wordpress.org/support/topic/awesome-4599/)󠁿

 [alexkone](https://profiles.wordpress.org/alexkone/) 9 de mayo de 2017

Definitely what I wanted. Essential for get images per posts. Thanks a lot

![](https://secure.gravatar.com/avatar/d90dd2fa2ed9335249d3570154021395875b5b6ec42ba7b9098601ed3307747c?
s=60&d=retro&r=g)

### 󠀁[It should be core in the next WordPress version!](https://wordpress.org/support/topic/it-should-be-core-in-the-next-wordpress-version/)󠁿

 [wpramones](https://profiles.wordpress.org/wpramones/) 14 de febrero de 2017

Makes retrieving post images much faster.

![](https://secure.gravatar.com/avatar/f54a626b61e61dfc6eb01049c02ffa8a79cf7a5ddc09bac0eed02a38769a0371?
s=60&d=retro&r=g)

### 󠀁[Should be core](https://wordpress.org/support/topic/should-be-core-8/)󠁿

 [alexwcoleman](https://profiles.wordpress.org/alexwcoleman/) 15 de enero de 2017

what else to say – it works. Should be core too.

![](https://secure.gravatar.com/avatar/1b3fee998f82a719f2469707bc7a90414e5123a2ed55ba2882077d1eaf3ed94e?
s=60&d=retro&r=g)

### 󠀁[Great extension for REST API](https://wordpress.org/support/topic/great-extension-for-rest-api/)󠁿

 [martinft](https://profiles.wordpress.org/martinft/) 3 de septiembre de 2016

The original REST API plugin has problems with featured images. This plugin is the
solution. Thank you very much for it!

![](https://secure.gravatar.com/avatar/e39154db84fbe81c1d80f91ea494906191dbd47d949df2830997eebc27247625?
s=60&d=retro&r=g)

### 󠀁[Perfect! Must be in the API core](https://wordpress.org/support/topic/perfect-must-be-in-the-api-core/)󠁿

 [eatzeni](https://profiles.wordpress.org/eatzeni/) 3 de septiembre de 2016

Does what id says, and it helps me a lot while building the theme in Angular2. Only
one GET to have Post and Featured Image details!

![](https://secure.gravatar.com/avatar/f910f1b1e01f259c6d8828c7f2c70b983a844d848bff4664910d8332ad948181?
s=60&d=retro&r=g)

### 󠀁[Good job](https://wordpress.org/support/topic/good-job-524/)󠁿

 [gladkoff1989](https://profiles.wordpress.org/gladkoff1989/) 3 de septiembre de
2016

saves time, thanks!

 [ Leer todas las 11 reseñas ](https://wordpress.org/support/plugin/better-rest-api-featured-images/reviews/)

## Colaboradores y desarrolladores

“Better REST API Featured Images” es un software de código abierto. Las siguientes
personas han colaborado con este plugin.

Colaboradores

 *   [ Braad ](https://profiles.wordpress.org/braad/)

“Better REST API Featured Images” ha sido traducido a 1 idioma. Gracias a [los traductores](https://translate.wordpress.org/projects/wp-plugins/better-rest-api-featured-images/contributors)
por sus contribuciones.

[Traduce “Better REST API Featured Images” a tu idioma.](https://translate.wordpress.org/projects/wp-plugins/better-rest-api-featured-images)

### ¿Interesado en el desarrollo?

[Revisa el código](https://plugins.trac.wordpress.org/browser/better-rest-api-featured-images/),
echa un vistazo al [repositorio SVN](https://plugins.svn.wordpress.org/better-rest-api-featured-images/),
o suscríbete al [log de desarrollo](https://plugins.trac.wordpress.org/log/better-rest-api-featured-images/)
por [RSS](https://plugins.trac.wordpress.org/log/better-rest-api-featured-images/?limit=100&mode=stop_on_copy&format=rss).

## Registro de cambios

#### 1.2.1

 * Add fix for bug caused by conflicts with plugins that manipulate image metadata

#### 1.2.0

 * Fix translation files present but not loading
 * Add note to the readme explaining that `?_embed` should be tried before using
   this plugin
 * Fix compat with older betas
 * Add missing PHPDoc statements
 * Tested with v2 beta 12

#### 1.1.1

 * Compatibility with v2 beta 11 of the REST API (now the core field is called featured_media;
   this plugin’s field is still better_featured_image). Props: filose

#### 1.1.0

 * Add a better_rest_api_featured_image filter for adding custom data to the response.
   Props: avishayil

#### 1.0.2

 * Change register_api_field to register_rest_field for compatibility with the REST
   API v2 beta 9. Props: Soean

#### 1.0.1

 * Switch to returning null instead of 0 when no featured image is present

#### 1.0.0

 * First Release

## Meta

 *  Versión **1.2.1**
 *  Última actualización **hace 10 años**
 *  Instalaciones activas **2.000+**
 *  Versión de WordPress ** 4.0 o superior **
 *  Testeado hasta **4.6.30**
 *  Idiomas
 * [Danish](https://da.wordpress.org/plugins/better-rest-api-featured-images/) y
   [English (US)](https://wordpress.org/plugins/better-rest-api-featured-images/).
 *  [Traducir a tu idioma](https://translate.wordpress.org/projects/wp-plugins/better-rest-api-featured-images)
 * Etiquetas:
 * [featured](https://es-ar.wordpress.org/plugins/tags/featured/)[images](https://es-ar.wordpress.org/plugins/tags/images/)
   [post](https://es-ar.wordpress.org/plugins/tags/post/)[rest](https://es-ar.wordpress.org/plugins/tags/rest/)
   [thumbnail](https://es-ar.wordpress.org/plugins/tags/thumbnail/)
 *  [Vista avanzada](https://es-ar.wordpress.org/plugins/better-rest-api-featured-images/advanced/)

## Valoraciones

 5 de 5 estrellas.

 *  [  11 valoraciones de 5 estrellas     ](https://wordpress.org/support/plugin/better-rest-api-featured-images/reviews/?filter=5)
 *  [  0 valoraciones de 4 estrellas     ](https://wordpress.org/support/plugin/better-rest-api-featured-images/reviews/?filter=4)
 *  [  0 valoraciones de 3 estrellas     ](https://wordpress.org/support/plugin/better-rest-api-featured-images/reviews/?filter=3)
 *  [  0 valoraciones de 2 estrellas     ](https://wordpress.org/support/plugin/better-rest-api-featured-images/reviews/?filter=2)
 *  [  0 valoraciones de 1 estrellas     ](https://wordpress.org/support/plugin/better-rest-api-featured-images/reviews/?filter=1)

[Your review](https://wordpress.org/support/plugin/better-rest-api-featured-images/reviews/#new-post)

[Ver todas las reseñas](https://wordpress.org/support/plugin/better-rest-api-featured-images/reviews/)

## Colaboradores

 *   [ Braad ](https://profiles.wordpress.org/braad/)

## Soporte

¿Tenés algo que decir? ¿Necesitás ayuda?

 [Ver el foro de asistencia](https://wordpress.org/support/plugin/better-rest-api-featured-images/)

## Donar

¿Te gustaría apoyar el progreso de este plugin?

 [ Doná a este plugin ](http://braadmartin.com/)