WordPress: YouTube e feed RSS
7 Maggio 2015
Questa mattina ho scoperto che il codice utilizzato da anni per visualizzare in WordPress i video recuperati da un feed RSS di YouTube ha smesso di funzionare. Invece di visualizzare il video corretto, ci si trova davanti a questo avviso.
Ecco il codice aggiornato e ripulito. Il videoId è disponibile direttamente nell’XML del feed.
<?php
include_once(ABSPATH . WPINC . '/feed.php');
$get_videos = function ($feed_data) {
$output = "<ul>\n";
if (! $feed_data) {
$output .= "<li>Nessun video disponibile</li>\n";
} else {
foreach ($feed_data as $item) {
$youtubetitle = $item->get_title();
$video_id = $item->get_item_tags(
'http://www.youtube.com/xml/schemas/2015',
'videoId'
);
$youtubeid = $video_id[0]['data'];
$output .= "<li>\n";
$output .= '<iframe width="240" height="176" src="https://www.youtube.com/embed/' .
$youtubeid . '?controls=0&showinfo=0" frameborder="0"></iframe>';
$output .= "<span>{$youtubetitle}</span>\n";
$output .= "</li>\n";
}
}
$output .= "</ul>\n";
return $output;
};
?>
<h3>Video</h3>
<?php
$url = **** URL del feed RSS****;
$rss = fetch_feed($url);
if (! is_wp_error($rss)) {
// Verifica se l'oggetto è stato creato correttamente
// Individua il numero di video presenti, al massimo leggine 3.
$maxitems = $rss->get_item_quantity(3);
// Crea un array di tutti gli elementi (il primo elemento ha indice 0).
$rss_items = $rss->get_items(0, $maxitems);
}
echo $get_videos($rss_items)
?>
Non è possibile inserire nuovi commenti. I commenti vengono disattivati automaticamente dopo 60 giorni.