blob: 4c34161ca171fb4bdf1f0ee3b490f18bb09685cb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<?php
/*
* Plugin Name: WPCOM Add Featured Media URL
*
* Adds `jetpack_featured_media_url` to post responses
*/
class WPCOM_REST_API_V2_Sites_Posts_Add_Featured_Media_URL {
function __construct() {
add_action( 'rest_api_init', array( $this, 'add_featured_media_url' ) );
}
function add_featured_media_url() {
register_rest_field( 'post', 'jetpack_featured_media_url',
array(
'get_callback' => array( $this, 'get_featured_media_url' ),
'update_callback' => null,
'schema' => null,
)
);
}
function get_featured_media_url( $object, $field_name, $request ) {
$featured_media_url = '';
$image_attributes = wp_get_attachment_image_src(
get_post_thumbnail_id( $object['id'] ),
'full'
);
if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
$featured_media_url = (string) $image_attributes[0];
}
return $featured_media_url;
}
}
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Sites_Posts_Add_Featured_Media_URL' );
|