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.
- To return an appropriate image, the second
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
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
25
26
27
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
36
if ($xml === false) return 'Error Loading RSS Feed.';
37
38
$xml = $xml['entry'];
39
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
52
53
$id = $url_vars['v'];
54
55
/* Get video dimensions */
56
57
58
$url = $video['url'];
59
60
if ($atts['width']) {
61
62
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
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
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)