Created
June 20, 2018 05:38
-
-
Save ahmedmusawir/e427323ee5622b7e1157f68f905318d6 to your computer and use it in GitHub Desktop.
PLUGIN DEV – RELATED POST W REST - ADDING FIELDS TO REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ADDING AUTHOR & IMAGE FIELDS TO REST | |
function related_post_register_fields() { | |
# Add Author Name | |
register_rest_field( 'post', | |
'author_name', | |
array( | |
'get_callback' => 'related_post_author_name', | |
'update_callback' => null, | |
'schema' => null | |
) | |
); | |
# Add Featured Image | |
register_rest_field( 'post', | |
'featured_image', | |
array( | |
'get_callback' => 'related_post_featured_image', | |
'update_callback' => null, | |
'schema' => null | |
) | |
); | |
} | |
function related_post_author_name( $object, $field_name, $request ) { | |
# code... | |
return get_the_author_meta( 'display_name' ); | |
} | |
function related_post_featured_image( $object, $field_name, $request ) { | |
# code... | |
$featured_image_array = wp_get_attachment_image( $object['featured_media'], 'thumbnail', true ); | |
// $featured_image_array = wp_get_attachment_image_src( $object['featured_media'], 'thumbnail', true ); | |
return $featured_image_array; | |
} | |
add_filter( 'rest_api_init', 'related_post_register_fields' ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment