[Solved] How to Reference/Echo Variable from Another PHP Function

Ryan Murphy Asks: How to Reference/Echo Variable from Another PHP Function
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:

Code:
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?

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.