RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts</strong | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.39% (6.59%*) • Fixed: 5.39% (6.59%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.48%*) • Investment PI: 5.39% (6.59%*)

Add Thumbnail Image (Links) from a YouTube RSS Feed to WordPress with Shortcode

Add Thumbnail Image (Links) from a YouTube RSS Feed to WordPress with Shortcode

This article will show you how to display YouTube thumbnail links in your WordPress website (or PHP application) by way of the video RSS feed. While another article is scheduled that'll display the same output using the YouTube API, the RSS method navigates the requirement to register for the API, making the process a little easier.

The block of YT code has undergone a numerous of variations because of the multitude of ways in which the data might be rendered. Because the thumbnail tower is essentially an image-feature, we once supported different methods of cropping and scaling the images we retrieved from YouTube... but that didn't suit everybody. What's published on this page is now a cupcake version - it's up to you to add the decoration. At the bottom of the page are a few options that might assist with image manipulation.

Note: More complete YouTube renderings can be built with version 3 of YouTube's API, and that information is forthcoming. Note that changes made to our backend may impact upon the examples (we'll tidy up formatting when time permits).

How To Find Your YouTube Channel RSS Feed

Your YouTube RSS feed can be found by navigating your way to https://www.youtube.com/feeds/videos.xml?channel_id=123, where 123 is your own channel ID. If you don't know what your Channel ID is, view the source for your YouTube page and search for the text videos.xml?channel_id - it will reveal the full XML feed. Alternatively, select your own channel from your subscriptions list and note string of text after channel. For example, the channel URL for BeliefMedia's YouTube page is https://www.youtube.com/channel/UC3qxAmFHsyqAnrg_UbbsHjQ. Our channel ID is UC3qxAmFHsyqAnrg_UbbsHjQ.

The Result

The shortcode of [ytrss channel="UC3qxAmFHsyqAnrg_UbbsHjQ" number="3"] will render the following:

The second example includes 3 random videos with the title text overlaid on the video thumbnail. The shortcode of [ytrss channel="UC3qxAmFHsyqAnrg_UbbsHjQ" number="3" random="1" type="1"] will return the following:

Facebook Ad Performance, Week 39, 2024
Facebook Ad Performance, Week 39, 2024
 
A Rant on AI in the Mortgage Industry
A Rant on AI in the Mortgage Industry
 
Now is the Time to Run Facebook Ads (for Mortgage Clients)
Now is the Time to Run Facebook Ads (for Mortgage Clients)
 

The final example illustrates how the feed might look if you applied some styling. Shortcode of [ytrss channel="UC3qxAmFHsyqAnrg_UbbsHjQ" 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"] returns the following abomination.

Another Sunny Saturday Visiting Display Homes in Sydney
Another Sunny Saturday Visiting Display Homes in Sydney
 
User SMS Verification with Yabber Website Forms
User SMS Verification with Yabber Website Forms
 

WordPress Shortcode

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

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

If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.

The second function on this page is required to return the appropriate thumbnail size. Additionally, the function below is necessary to resize images (discussed below).

1
<?php 
2
/*
3
 Returns new width and height via dodgy scaling of YouTube thumbnail
4
 http://www.beliefmedia.com/youtube-rss-tower-wordpress
5
*/
6
 
7
function beliefmedia_youtube_thumbnail_size($thumbnail, $width, $currentWidth) {
8
  $hd = array('5', '8');
9
  $result = array();
10
  $factor = (in_array($thumbnail, $hd)) ? 1.777 : 1.333;
11
 return array('width' => $width, 'height' => round($width / $factor));
12
}

Resizing Thumbnails

The required function referenced on this page lists the most commonly used YouTube thumbnails. The only two thumbnails that return the 16:9 thumbnail (or those that are returned without the top and bottom black bars) are the mqdefault (320 x 180) and maxresdefault (1920 x 1080) images. If you're not using a library to scale remote images automatically, the mqdefault image can easily be scaled down without too many issues.

If you're not too concerned about importing all your YouTube images into your own WordPress library, you can create your own 16:9 (1.777) cropped image size and use the function on this page to create appropriately scaled thumbnails... although this does require slight modification to the code.

Shortcode Attributes

channel

Your YouTube channel.

number

The number of thumbnail results to return. The YouTube RSS feeds returns only your last 15 uploads.

thumbnail

The thumbnail ID references the image sizes as detailed on this page. The mqdefault (320 x 180) image is referenced by default.

width

If you choose to use a width other than the actual size of the image, enter it here. The image will be scaled down and the height will automatically be determined.

random

To return random videos, use random="1".

style

The style attribute applies style to the parent div container. By default the container will match the width as defined by the width attribute or the width of the referenced image.

span

To add styling to the title text, add it with the span attribute. By default we use font-size: 0.9em; font-weight: bold;.

words

To truncate the title text to a specific number of words, use words="15" (where 15 is the maximum number of words to return).

type

For text underneath the image, use text="0". To overlay the text on the thumbnail, use text="1".

cache

The period of time to cache the result. Defaults to 18 hours.

PHP Function

Used outside of WordPress, the function on this page may be used. As with the shortcode, it requires the second beliefmedia_youtube_thumbnail() function on this page. It also requires Simple Cache and the beliefmedia_youtube_thumbnail_size() function above.

Download


Title: Add Thumbnail Image Links from a YouTube RSS Feed (WP Shortcode)
Description: Add Thumbnail Image Links from a YouTube RSS Feed With WordPress Shortcode or PHP.
  Download • Version 0.2, 1.4K, zip, Category: WordPress Shortcodes
PHP Code & Snippets, (2.4K)    

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

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

Leave a comment