RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Add Thumbnail Images from a YouTube RSS Feed in a Tower With PHP

The code on this page is to support the article here titled "Add Thumbnail Image (Links) from a YouTube RSS Feed to WordPress with Shortcode". That article should be referenced for information on the available argument array and formatting options.

The function requires three additional supporting functions:

  • For truncating a title to a number of words, use beliefmedia_truncate_words() from this page (the function has the title "Truncate to a Word Count").
  • To return an appropriate image, the second beliefmedia_youtube_thumbnail() function is required from this page, titled "Get YouTube Thumbnail With PHP".
  • The beliefmedia_youtube_thumbnail_size() function as provided on our shortcode article is required.
  • Simple Cache is required to cache the returned result. Get it here.

PHP Code

1
<?php 
2
include('../simple-cache/cache.inc.php');
3
 
4
/*
5
 Add Thumbnail Image (Links) from a YouTube RSS Feed to WordPress with Shortcode
6
 http://www.beliefmedia.com/youtube-rss-tower-wordpress
7
*/
8
 
9
function beliefmedia_youtube_tower_rss($channel, $args = '') {
10
 
11
  $atts = array(
12
    'number' => '4',
13
    'thumbnail' => '5',
14
    'width' => '280',
15
    'random' => 0,
16
    'style' => 0,
17
    'span' => 0,
18
    'words' => 0,
19
    'type' => '0',
20
    'cache' => 18 * 3600
21
  );
22
 
23
  /* Merge $args with $atts */
24
  $atts = (empty($args)) ? $atts : array_merge($atts, $args);
25
 
26
  $transient = 'ytrss_' . md5(serialize($atts));
27
  $cached = beliefmedia_get_transient($transient, $atts['cache']);
28
 
29
  if ($cached !== false) {
30
    return $cached;
31
 
32
  } else {
33
 
34
  $rss = 'https://www.youtube.com/feeds/videos.xml?channel_id=' . $channel;
35
  $xml = (array) @simplexml_load_file($rss);
36
  if ($xml === false) return 'Error Loading RSS Feed.';
37
 
38
  $xml = $xml['entry'];
39
  if ($atts['random']) shuffle($xml);
40
 
41
    for ($i = 0; $i < $atts['number']; $i++) {
42
 
43
       $title = $xml["$i"]->title;
44
       if ($atts['words']) $title = beliefmedia_truncate_words($title, $atts['words'], $trimmarker = '..');
45
 
46
       $attributes = $xml["$i"]->link->attributes();
47
       $href = $attributes['href'];
48
       $pubdate = $xml["$i"]->published;
49
 
50
       /* Get video ID */
51
       $link = html_entity_decode($href);
52
       parse_str( parse_url( $link, PHP_URL_QUERY ), $url_vars );
53
       $id = $url_vars['v'];
54
 
55
       /* Get video dimensions */
56
       $video = beliefmedia_youtube_thumbnail($id, $atts['thumbnail']);
57
       $w = round($video['width']); $height = round($video['height']);
58
       $url = $video['url'];
59
 
60
       if ($atts['width']) {
61
         $video = beliefmedia_youtube_thumbnail_size($atts['thumbnail'], $atts['width'], $w);
62
         $w = round($video['width']); $height = round($video['height']);
63
       }
64
 
65
       switch ($atts['type']) {
66
 
67
          case '0':
68
 
69
              /* Default span CSS */
70
              if (!$atts['span']) $atts['span'] = 'font-size: 0.9em; color: white; font-weight: bold;';
71
 
72
              $return .= '<a href="' . $link . '" target="_blank" rel="noopener noreferrer"><img src="' . $url . '" width="' . $w . '" height="' . $height . '" alt="' . $title . '" title="' . $title . '"></a>';
73
              $return .= '<div style="padding-top: 10px; padding-bottom: 20px;"><span style="' . $atts['span'] . '"><a href="' . $link . '" target="_blank" title="' . $title . '" rel="noopener noreferrer">' . $title . '</a></span></div>';
74
              break;
75
 
76
          case '1':
77
 
78
              /* Default span CSS */
79
              if (!$atts['span']) $atts['span'] = 'color: white; font-size: 0.9em; font-weight:bold; background:rgba(0,0,0,0.6); padding: 2px;';
80
 
81
              $return .= '<div style="height: ' . $height . 'px; width:' . $w . 'px; position: relative;">';
82
              $return .= '<a href="' . $link . '" target="_blank" rel="noopener noreferrer"><img style="position:absolute; left:0; top:0;" src="' . $url . '" width="' . $w . '" height="' . $height . '" alt="' . $title . '" title="' . $title . '" /></a>';
83
              $return .= '<div style="z-index:1; position: absolute;  padding: 15px;"><span style="' . $atts['span'] . '">' . $title . '</span></div>';
84
              $return .= '</p></div>';
85
              $return .= '<div style="height: 12px; width: ' . $w . 'px;"> </div>';
86
              break;
87
 
88
       }
89
 
90
    }
91
 
92
   $result = '<div style="width: ' . $w . 'px; margin: 0 auto;';
93
   if ($atts['style']) $result .= $atts['style'];
94
   $result .= '">' . $return . '</div>';
95
 
96
   /* Set transient */
97
   beliefmedia_set_transient($transient, $result);
98
 
99
  return $result;
100
 }
101
}

Usage is as follows:

1
<?php 
2
/* Usage */
3
echo beliefmedia_youtube_tower_rss($channel = 'UC3qxAmFHsyqAnrg_UbbsHjQ');
4
 
5
/* Usage with function args */
6
$args = array('number' => '2', 'random' => '1', 'type' => '1', 'width' => '300', 'style' => 'border: 5px dotted; background: #ff0000; padding-left: 10px; padding-right: 10px; padding-top: 10px;', 'span=' => 'font-size: 1.2em; font-weight: bold; background: #FFFF80; color: #000000');
7
echo beliefmedia_youtube_tower_rss($channel = 'UC3qxAmFHsyqAnrg_UbbsHjQ', $args);

Download


Title: YouTube Responsive Gallery or Tower in WordPress (Shortcode)
Description: Create a Responsive Gallery (or Tower) of YouTube Thumbnails in WordPress (Using YouTube's V3 API).
  Download • Version 0.2, 2.1K, zip, Category: WordPress Shortcodes
PHP Code & Snippets, (2.4K)    

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment