Hy im building the Trailer section, i want to get the time automate from Youtube video's...

Custom Fields

  • $key="trailer"; - represents the key of the trailer Ex (http://www.youtube.com/watch?v=QiNIfGiNiOk were key= QiNIfGiNiOk)
  • $key="img_trailer"; - is the img of youtube 0.jpg
  • $key="time"; - is the time of the youtube videos, this i enter manual

The question "Can i make it work to retrive the youtube time automate? and how can i make the code use less query possible"

This is my code:

$recentPosts = new WP_Query();
$recentPosts-> query(array('showposts' => 4,'orderby' => 'rand', 'post_type' =>array('trailers')));
<div class="art-Post" >
    <div class="art-Post-tl"></div>
    <div class="art-Post-tr"></div>
    <div class="art-Post-bl"></div>
    <div class="art-Post-br"></div>
    <div class="art-Post-tc"></div>
    <div class="art-Post-bc"></div>
    <div class="art-Post-cl"></div>
    <div class="art-Post-cr"></div>
    <div class="art-Post-cc"></div>
    <div class="art-Post-body">

<h2 id="titlu-descriere" class="titlu-d">Trailere care s-ar putea sa-ti placa</h2>
<div class="linie-total"></div>   
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
<div class="box-trailere">
<div class="trailer-img">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Trailer film %s', 'kubrick'), the_title_attribute('echo=0')); ?>">
<img src="http://img.youtube.com/vi/<?php $key="trailer"; echo get_post_meta($post->ID, $key, true); ?>/<?php $key="img_trailer"; echo get_post_meta($post->ID, $key, true); ?>">
<span class="duration"><?php $key="time"; echo get_post_meta($post->ID, $key, true); ?></span>
<span class="trailer-title"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></span>
<?php endwhile; ?>

<div class="cleared"></div>

------------------------------- UPDATE -------------------

The down code it retrive me this

stdClass Object
    [apiVersion] => 2.1
    [data] => stdClass Object
            [updated] => 2013-02-27T18:12:03.984Z
            [totalItems] => 1
            [startIndex] => 1
            [itemsPerPage] => 25
            [items] => Array
                    [0] => stdClass Object
                            [id] => QiNIfGiNiOk
                            [uploaded] => 2012-11-30T18:20:08.000Z
                            [updated] => 2013-02-26T22:33:56.000Z
                            [uploader] => ramonsteck
                            [category] => Entertainment
                            [title] => After Earth - Trailer 2013
                            [description] => Unete www.facebook.com/Cinemovs Historias Paranormales Reales, Películas de Terror Online y proximos estrenos de cine.
                            [thumbnail] => stdClass Object
                                    [sqDefault] => http://i.ytimg.com/vi/QiNIfGiNiOk/default.jpg
                                    [hqDefault] => http://i.ytimg.com/vi/QiNIfGiNiOk/hqdefault.jpg

                            [player] => stdClass Object
                                    [default] => https://www.youtube.com/watch?v=QiNIfGiNiOk&feature=youtube_gdata_player
                                    [mobile] => https://m.youtube.com/details?v=QiNIfGiNiOk

                            [content] => stdClass Object
                                    [5] => https://www.youtube.com/v/QiNIfGiNiOk?version=3&f=videos&app=youtube_gdata
                                    [1] => rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQnpiI1ofEgjQhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp
                                    [6] => rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQnpiI1ofEgjQhMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp

                            [duration] => 123
                            [aspectRatio] => widescreen
                            [rating] => 2.6722891
                            [likeCount] => 174
                            [ratingCount] => 415
                            [viewCount] => 247218
                            [favoriteCount] => 0
                            [commentCount] => 63
                            [accessControl] => stdClass Object
                                    [comment] => allowed
                                    [commentVote] => allowed
                                    [videoRespond] => moderated
                                    [rate] => allowed
                                    [embed] => allowed
                                    [list] => allowed
                                    [autoPlay] => allowed
                                    [syndicate] => allowed





These two functions here will grab data from the YouTube API about a video:

    // function to parse the code from the URL
    function parse_youtube_video_id_from_url( $video_url ) {
        $splited_data = explode( '=', $video_url );
        $video_unique_code = substr( $splited_data[1], 0, -strlen( strrchr( $splited_data[1], '&' ) ) );
        return $video_unique_code;

    // use the youtube APIs to get a json string containing data about the video
    function get_youtube_json_video_data( $video_code ) {
        $api_url = "https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q={$video_code}";
        $data = wp_remote_get( $api_url );
        return wp_remote_retrieve_body( $data );

You must use these to get the time data

So for your example, here's how you'd get the data:

$youtubecode = 'QiNIfGiNiOk'; // replace with your youtube code or variable etc etc..
$video_data = get_youtube_json_video_data($youtubecode);
$video_data = json_decode( $video_data );

Then finally, to see what data you got, let's pass the video_data into print_r to put some debug output on the screen:

// lets print out the data so we can see what we got:
printf( '<pre>%s</pre>', var_export( $video_data, true ) );

You should see a pre tag containing a PHP data structure with everything you could ever want to know about the YouTube video (assuming the video exists).

And there you go, $video_data should contain the time, description, uploader, all the thumbnails, ratings, etc

You would then use this new found knowledge to create a function, hook that function into the 'save_post' and 'publish_post' actions, that updates the post meta/custom field based on the above code

How to use filters and actions, or how to grab from or save to a custom field, is beyond the scope of your question.

  • how can i implement that in my code, the function are posted in Functions.php. And the rest were do i put it ? – Batman Feb 27 '13 at 16:03
  • Implement which exactly? I described what you would need to do in the answer, what exactly do you require knowledge of? Hooks? Actions? I'm assuming you've tested the code out somewhere it can output to the frontend and looked at the contents of $video_data ? – Tom J Nowell Feb 27 '13 at 17:50
  • 2
    I've made modifications to my answer to make things clearer, yet I sense that your lack of general PHP knowledge, and your lack of a knowledge of actions and filters prevents you from using any answer given here. These you will need to research ( or ask a new question about that specific subject ). For me to provide a copy paste solution requiring no PHP knowledge and only basic text editing skills would be the same as me doing your work for you and you learning nothing. – Tom J Nowell Feb 27 '13 at 18:13
  • 1
    You have everything you need already, just look at the big data structure it printed out, e.g. $video_data->data->updated is when it was last updated, etc – Tom J Nowell Feb 28 '13 at 10:00
  • 1
    Update For later readers: This is the follow up question. – kaiser Apr 15 '13 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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