1

I have successfully set up a PHP function for my blog posts to create a shortcode to automatically display/style an iFrame tag for a YouTube video, as well as pull in the duration and view count immediately underneath.

I am now looking to display JUST the view count and duration values for the video on the home/list pages (via loop.php) but having difficulty determining the appropriate PHP code to do this.

Here is the function that I've been using successfully for the post view:

add_shortcode('yt', 'getYoutubeDetails');

function getYoutubeDetails($atts) {
    extract(shortcode_atts(array(
    'video' => ''
    ), $atts));

    // Get YouTube data via the API
    $JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos?q=$video&alt=json");
    $JSON_Data = json_decode($JSON);
    $views = $JSON_Data->{'feed'}->{'entry'}[0]->{'yt$statistics'}->{'viewCount'};
    $views = number_format($views);
    $duration = $JSON_Data->{'feed'}->{'entry'}[0]->{'media$group'}->{'yt$duration'}->{'seconds'};


    echo "<iframe width=\"1280\" height=\"720\" src=\"//www.youtube.com/embed/$video?showinfo=0&rel=0&modestbranding=1&theme=light&iv_load_policy=3&autohide=1&enablejsapi=1\" frameborder=\"0\" allowfullscreen></iframe>";
    echo "<strong>Views:</strong> $views<br><strong>Duration:</strong> ";
    echo sec2hms($duration);
    echo "<br /><br />";
    }

Within my post content, I tag my videos as [yt video="VIDEOID"]. I was hoping I could get the specific attribute for views/duration by using: <?php echo GetYoutubeDetails($views) ?> in loop.php, but that doesn't seem to work. Is there a way to structure this to pull in a specific attribute within the function? Would it be able to leverage the value being passed into the video attribute of the shortcode that's already in the post content?

I'd prefer to tap into the existing shortcode already in the post content (so I'm not having to maintain it two or more different places). However, if there isn't a way to echo the value of an existing function for a particular post in loop.php, I was thinking it may need to get handled via a custom field somehow.

Any suggestions for how to tackle this issue?

2
  • 1
    The call you are making to the function has a spelling mistake. Also, if I remember correctly, you shouldnt be echoing in the function, just use return.
    – Tomás Cot
    Aug 13, 2014 at 1:54
  • Output from a shortcode should be returned, not echoed Aug 13, 2014 at 7:43

1 Answer 1

1

As indicated in the comments, you should return the string, not echo it:

add_shortcode('yt', 'getYoutubeDetails');

function getYoutubeDetails($atts) {
    extract(shortcode_atts(array(
    'video' => ''
    ), $atts));

    // Get YouTube data via the API
    $JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos?q=$video&alt=json");
    $JSON_Data = json_decode($JSON);
    $views = $JSON_Data->{'feed'}->{'entry'}[0]->{'yt$statistics'}->{'viewCount'};
    $views = number_format($views);
    $duration = $JSON_Data->{'feed'}->{'entry'}[0]->{'media$group'}->{'yt$duration'}->{'seconds'};


    $s = "<iframe width=\"1280\" height=\"720\" src=\"//www.youtube.com/embed/$video?showinfo=0&rel=0&modestbranding=1&theme=light&iv_load_policy=3&autohide=1&enablejsapi=1\" frameborder=\"0\" allowfullscreen></iframe>";
    $s .= "<strong>Views:</strong> $views<br><strong>Duration:</strong> ";
    $s .= sec2hms($duration);
    $s .= "<br /><br />";
    return $s;
}
1
  • Thanks for the response. I updated the getYoutubeDetails function to use the return instead of echo. However, I still need assistance with pulling the view count and duration values into the post loop. Can anyone provide some guidance? Aug 20, 2014 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.