Skip to content

Instantly share code, notes, and snippets.

@imjn
Created November 23, 2018 03:05
Show Gist options
  • Save imjn/bbfad6528b0e4a55c77b4f3c974bd7de to your computer and use it in GitHub Desktop.
Save imjn/bbfad6528b0e4a55c77b4f3c974bd7de to your computer and use it in GitHub Desktop.
wp get featured image
add_action('rest_api_init', 'register_rest_images' );
function register_rest_images(){
    register_rest_field( array('post'),
        'fimg_url',
        array(
            'get_callback'    => 'get_rest_featured_image',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}
function get_rest_featured_image( $object, $field_name, $request ) {
    if( $object['featured_media'] ){
        $img = wp_get_attachment_image_src( $object['featured_media'], 'app-thumb' );
        return $img[0];
    }
    return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment