Skip to content

Commit b08ae5b

Browse files
Merge pull request #89 from MITLibraries/lm304
Update experts API endpoint to contain image URLs in featured_media field
2 parents 168d7fe + ce6a77a commit b08ae5b

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

web/app/themes/mitlib-parent/functions.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ function customize_preview_js() {
262262
* @link http://v2.wp-api.org/extending/modifying/
263263
* @link https://gist.github.com/rileypaulsen/9b4505cdd0ac88d5ef51#gistcomment-1622466
264264
*/
265-
function mitlib_api_alter() {
265+
function api_alter() {
266266
// Add custom fields to posts endpoint.
267267
register_rest_field(
268268
'post',
@@ -278,8 +278,19 @@ function mitlib_api_alter() {
278278
'schema' => null,
279279
)
280280
);
281+
282+
// Switch featured_media field from media ID to URL of the image.
283+
register_rest_field(
284+
'experts',
285+
'featured_media',
286+
array(
287+
'get_callback' => 'Mitlib\Parent\api_get_image',
288+
'update_callback' => null,
289+
'schema' => null,
290+
)
291+
);
281292
}
282-
add_action( 'rest_api_init', 'Mitlib\Parent\mitlib_api_alter' );
293+
add_action( 'rest_api_init', 'Mitlib\Parent\api_alter' );
283294

284295
/**
285296
* Registers our main widget areas.
@@ -418,6 +429,20 @@ function customize_body_class( $classes ) {
418429
* may be called by the templates within the theme.
419430
*/
420431

432+
/**
433+
* Get the value of a specified field for use in the API. This function is
434+
* called by api_alter above
435+
*
436+
* @param array $object Details of current post.
437+
* @param string $field_name Name of field.
438+
*
439+
* @return mixed
440+
*/
441+
function api_get_image( $object, $field_name ) {
442+
$link = wp_get_attachment_image_src( $object[ $field_name ], 'thumbnail-size' );
443+
return $link[0];
444+
}
445+
421446
/**
422447
* Provides an alternative way for building a breadcrumb.
423448
*/

0 commit comments

Comments
 (0)