plugins – Thumbnail image doesn’t show up in Shortcode output

Question

A ‘WordPress Shortcode‘ is being developed to display ‘YouTube Channel Thumbnail’ and ‘Latest Playlist Thumbnail’ when supplied with ‘YouTube Channel ID’ as follows.

[yt_insert channel_id="UCbCmjCuTUZos6Inko4u57UQ"]

enter image description here

Shortcode implementation is as follows.

function yt_insert_decode( $atts ) {
    $atts = shortcode_atts( array(
        'channel_id' => ''
    ), $atts );

    $api_key = 'YOUTUBE_API_KEY';
    $channel_id = $atts['channel_id'];
    $api_url="https://www.googleapis.com/youtube/v3/channels?part=snippet,contentDetails&id=" 
    . urlencode($channel_id) . '&key=' . $api_key;

    $api_response = wp_remote_get($api_url);
    $api_response = json_decode( wp_remote_retrieve_body($api_response) );

    $channel_thumbnail = $api_response->items[0]->snippet->thumbnails->default->url;
    $playlist_id = $api_response->items[0]->contentDetails->relatedPlaylists->uploads;

    $playlist_endpoint="https://youtube.googleapis.com/youtube/v3/playlists?part=snippet&id="
    . urlencode($playlist_id) . '&key=' . $api_key;

    $api_response = wp_remote_get($playlist_endpoint);
    $api_response = json_decode( wp_remote_retrieve_body($api_response) );

    $playlist_thumbnail = $api_response->items[0]->snippet->thumbnails->default->url;

    $response="<img src="" . $channel_thumbnail . '"> <img src="' . $playlist_thumbnail . '"> ';
    echo $response;

}

add_shortcode( 'yt_insert', 'yt_insert_decode' );

‘Channel Thumbnail’ DOES NOT show up while ‘Playlist Thumbnail’ shows up.

enter image description here

How can I change the code so that ‘YouTube Channel Thumbnail’ shows up in my Shortcode output?

in progress 0
Praveen Dias 2 months 2021-08-04T14:02:06-05:00 0 Answer 0 views 0

Answer ( 1 )

    0
    2021-08-05T12:58:17-05:00

    Have you done something like var_dump( $api_response->items[0]->snippet->thumbnails->default->url ) ); to make sure the API is returning the data you expect it to?

Leave an answer

Browse
Browse