go41

YouTube user channel rss feed import without API key

von Joern am 9. Mai. 2015 | Keine Kommentare

How to import the latest videos of a Youtube Channel to your sidebar.
With youtube api v3 there are also some changes to the rss feed, so the long time used way to import the feed with player is not working anymore, you just get a thumbnail titled ‚https://youtube.com/devicesupport‘ , nothing else.

Actually I do not need the player loaded on my site (needs API key). The title and a preview image with the link to the respective video on youtube is enough, right?
So we do not need an API key if we just get title and image, without any player.
To use youtube api v3 with a player you must create an API key!!

So I modified the code (working before with the old api version 2 ‚v2‘) we published here in 2010 ( Add a YouTube user channel video to WordPress http://forum.go41.de/viewtopic.php?f=1&t=75)

We are going to use a function built into WordPress, the ‚class simplepie‘.

On top of the function below we load SimplePie if it is not loaded already.

UPDATE IN NEXT POST – USE THE CODE IN NEXT POST TO GET THE SAME RESULT

Here the code I use in the moment, not nice, but working

Code:

USE UPDATED CODE PLEASE
<aside class="widget">
<h3 class="widget-title">Youtube Channel</h3>

<?php if (!class_exists('SimplePie')) { // load simplepie in functions here
if ( file_exists(ABSPATH . WPINC . '/class-simplepie.php') ) {
            @require_once (ABSPATH . WPINC . '/class-simplepie.php'); }
            }
if (class_exists('SimplePie')) {
$feed = new SimplePie();
$feed->set_feed_url("https://www.youtube.com/feeds/videos.xml?channel_id=YOUR CHANNEL ID");
$feed->set_stupidly_fast(true);
$feed->enable_cache(false); //  disable caching
$feed->set_timeout(5);
$success = $feed->init();
$feed->handle_content_type();
if ($success): $entry = 0; ?>
<ul>
<?php foreach ($feed->get_items(0, 3) as $item): // just loading the latest 3 items
if ($item) {  ?>
<li>
<a href="<?php echo $item->get_permalink(); ?>" title="<?php echo $item->get_title(); ?>"  target="_blank"><?php echo $item->get_title(); ?></a>
<?php
/*         
in feed looks like this:    <media:thumbnail url="https://i3.ytimg.com/vi/video-ID/hqdefault.jpg" width="480" height="360"/>
we could use the 480w image like this<img src="https://i3.ytimg.com/vi/video-ID/hqdefault.jpg" alt="Channel Name" width="480" height="360" />
feed has no description..         <?php echo $item->get_description(); ?>
*/
if ($enclosure = $item->get_enclosure()) // we get the thumbnail image link
    {
   $thumbnail = $enclosure->get_thumbnail();
// echo $thumbnail; Test only
   $thumbnail = substr($thumbnail, 0,-13); // cut off 'hqdefault.jpg' and add new filename below
   $thumb1 = $thumbnail."1.jpg"; // 1.jpg is a 120 wide image, Medium Quality Thumbnail (320x180 pixels) would be mqdefault.jpg
    echo '<p><a href="' . $item->get_permalink() . '" target="_blank" title="' . $item->get_title() . '"> <img src="' . $thumb1 . '" width="120" height="90" alt="' . $item->get_title() . '"/></a></p>';
    }
   ?>
<p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p>
<?php } // end item ?>
</li>
<?php endforeach; ?></ul><?php endif; //success ?>
<?php $feed->__destruct(); unset($item); unset($feed); ?>
<?php } // end Simplepie ?>
</aside>

This code will not write anything to your database or server.

BUT BACKUP ANY TEMPLATE FILE YOU MODIFY (KEEP THE ORIGINAL)

sample of above code you find in the footer of
http://www.linedancestompers.de/
____________________
you find me on Google+, Twitter and Facebook

(von: Joern)

Hier noch 1 weitere Ergebnisse dieses Threads:

UPDATE: YouTube user channel rss feed import without API key :: Reply by Joern

9. Mai. 2015 (von: Joern)

Same YouTube user channel feed import as above, but totally modified code using the WordPress function fetch_feed( $uri );on Google+ Thomas Scholz (toscho) https://plus.google.com/115532307838378431827 asked me to sanitize the output and cache the result. Thank you!fetch_feed caches for 12 hours…

Autor:

Du findest mich auch auf Google+, Twitter und Facebook!

Schreibe einen Kommentar

Pflichtfelder sind mit * markiert.


weitere forum Beiträge: