• jamesgreenhouse

    (@jamesgreenhouse)


    I searched everywhere for this code and could only find out how to add thumbnails of a user’s most recent youtube video, not the actual video, so I ended having to write my own code.

    Paste this code where you want it in your theme files.

    It uses the user’s youtube RSS feed (replace “USERNAME” in the code below with your youtube username).
    I got a lot of this code from perishable press’s superb post about pulling in RSS feeds here so credit to them.

    You’ll see a pretty standard youtube embed code in the code below which i edited to fit into the project i was working on so you’ll need to edit it yourself to fit your own requirements.

    Hope someone finds this useful.

    <?php
    include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
    $feed = fetch_rss('https://gdata.youtube.com/feeds/base/users/example/uploads?orderby=updated&alt=rss&v=2&client=ytapi-youtube-rss-redirect'); // specify feed url
    $items = array_slice($feed->items, 0, 1); // specify first and last item
    ?>
    
    <?php if (!empty($items)) : ?>
    <?php foreach ($items as $item) : ?>
    
    <object width="96%"><param name="movie" value="<?php echo str_replace("watch?v=","v/", $item['link']); ?><?php echo str_replace("&feature=youtube_gdata","", $item['link']);?>&hl=en_GB&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="<?php echo str_replace("watch?v=","v/", $item['link']); ?><?php echo str_replace("&feature=youtube_gdata","", $item['link']);?>&hl=en_GB&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="96%"></embed></object>
    
    <?php endforeach; ?>
    <?php endif; ?>
  • The topic ‘Embed latest youtube video’ is closed to new replies.