Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggregation Enquires Welcome

Example Interest Rates: Home Loan Variable: 2.54% (2.74%*) • Home Loan Fixed: 3.74% (3.48%*) • Fixed: 2.48% (2.94%*) • Variable: 2.54% (2.74%*) • Investment IO: 2.3% (3.76%*) • Investment PI: 2.04% (3.8%*)

YouTube Subscribe Button With WordPress Shortcode or PHP

YouTube Subscribe Button With WordPress Shortcode or PHP

The YouTube Subscribe button shortcode permits you to embed a channel subscription button into your WordPress post or page with shortcode. The button is another good argument for shortcode over code snippets provided by online tools; if the YouTube embed code ever changes, or new features were added, or you simply wanted to alter its appearance, modifying a single function would alter every occurrence of your subscription button on your website. YouTube has altered their code in the past... and they may do again.

The Result

The shortcode of [ytsubscribe layout="full"] will display the following (we've used the full layout for our Flight YT channel).

To display only the subscription button without the count, use [ytsubscribe count="hidden" align="right"]. In this case, we've also aligned the button to the right. This will apply a little CSS with padding (defined as a shortcode attribute). We could have optionally aligned left with [ytsubscribe count="hidden" align="left"]. The button is centered and wrapped in paragraph tags by default.

In the final example, we'll use a full layout in a 'dark' theme for BeliefMedia with the shortcode of [ytsubscribe theme="dark" layout="full" id="UC3qxAmFHsyqAnrg_UbbsHjQ"]. Note that I've used the attribute of id. If I were rendering a badge for a channel by name I would have used [ytsubscribe theme="dark" layout="full" channel="beliefmedia"]. The result:

We've only just started posting videos to our (lonely) company YouTube channel . While you're here, consider subscribing!

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. You may optionally download and install our plugin from the bottom of of the page.

1
<?php 
2
/*
3
 YouTube Subscribe Button With WordPress Shortcode or PHP
4
 http://www.beliefmedia.com/youtube-subscribe-button
5
*/
6
 
7
8
 
9
  $atts = shortcode_atts(array(
10
    'channel' => 'flightorg',
11
    'id' => '',
12
    'layout' => 'default', /* default or full */
13
    'count' => 'default', /* default or hidden */
14
    'theme' => 'default', /* default or dark */
15
    'listen' => false,
16
    'align' => false,
17
    'padding' => '8px 15px 8px 15px'
18
  ), $atts);
19
 
20
   $return = '<script src="https://apis.google.com/js/platform.js"></script>';
21
 
22
   if ($atts['listen'] !== false) {
23
 
24
   /* listen JavaScript */
25
   $return .= "<script>
26
      function onYtEvent(payload) {
27
        if (payload.eventType == 'subscribe') {
28
          // Add code to handle subscribe event.
29
        } else if (payload.eventType == 'unsubscribe') {
30
          // Add code to handle unsubscribe event.
31
        }
32
        if (window.console) { // for debugging only
33
          window.console.log('YT event: ', payload);
34
        }
35
      }
36
    </script>";
37
 
38
   }
39
 
40
   /* Use ID over channel */
41
   $channel = ($atts['id'] != '') ? 'data-channelid="' . $atts['id'] . '"' : 'data-channel="' . $atts['channel'] . '"';
42
 
43
   /* Build button */
44
   $css = ($atts['align'] !== false) ? 'float: ' . $atts['align'] . '; padding:' . $atts['padding'] : 'text-align:center;';
45
   $return .= '<div style="' . $css . '">
46
<div class="g-ytsubscribe" ' . $channel . ' data-layout="' . $atts['layout'] . '" data-count="' . $atts['count'] . '" data-theme="' . $atts['theme'] . '"';
47
   if ($atts['listen'] !== false) $return .= ' data-onytevent="onYtEvent"';
48
   $return .= '></div>
49
<div style="clear: both;"></div>
50
</div>';
51
 
52
 /* Return button */
53
 return ($atts['align'] === false) ? '' . $return . '' : $return;
54
}
55
add_shortcode('ytsubscribe', 'beliefmedia_youtube_subscribe_button');

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.

Shortcode Attributes

The following attributes alter the behaviour of your subscription button.

channel

Your YouTube channel name. If an id is set, the channel will will be overwritten.

id

Your channel ID. A channel id will overwrite your channel name.

layout

The layout is either default (just a button) or full. Defaults to default.

count

By default your subscriber count will show. To hide, use count="hidden".

theme

The two theme options are default and dark.

listen

By default listen is false. If you build in some JavaScript listeners the code is waiting for you (use listen="1").

align

By default the badge will align to the center in paragraph tags. To float left or right use align="right".

padding

If you align your badge to the left or right, padding applies. Defaults to 8px 15px 8px 15px. Alter as necessary.

PHP Function

Used outside of WordPress, the following may be used.

1
<?php 
2
/*
3
 YouTube Subscribe Button With WordPress Shortcode or PHP
4
 http://www.beliefmedia.com/youtube-subscribe-button
5
*/
6
 
7
function beliefmedia_youtube_subscribe_button($args = '') {
8
 
9
  $atts = array(
10
    'channel' => 'flightorg',
11
    'id' => '',
12
    'layout' => 'default', /* default or full */
13
    'count' => 'default', /* default or hidden */
14
    'theme' => 'default', /* default or dark */
15
    'listen' => false,
16
    'align' => false,
17
    'padding' => '8px 15px 8px 15px'
18
  );
19
 
20
  /* Merge $args with $atts */
21
  $atts = (empty($args)) ? $atts : array_merge($atts, $args);
22
 
23
   $return = '<script src="https://apis.google.com/js/platform.js"></script>';
24
 
25
   if ($atts['listen'] !== false) {
26
 
27
   /* listen JavaScript */
28
   $return .= "<script>
29
      function onYtEvent(payload) {
30
        if (payload.eventType == 'subscribe') {
31
          // Add code to handle subscribe event.
32
        } else if (payload.eventType == 'unsubscribe') {
33
          // Add code to handle unsubscribe event.
34
        }
35
        if (window.console) { // for debugging only
36
          window.console.log('YT event: ', payload);
37
        }
38
      }
39
    </script>";
40
 
41
   }
42
 
43
   /* Use ID over channel */
44
   $channel = ($atts['id'] != '') ? 'data-channelid="' . $atts['id'] . '"' : 'data-channel="' . $atts['channel'] . '"';
45
 
46
   /* Build button */
47
   $css = ($atts['align'] !== false) ? 'float: ' . $atts['align'] . '; padding:' . $atts['padding'] : 'text-align:center;';
48
   $return .= '<div style="' . $css . '">
49
<div class="g-ytsubscribe" ' . $channel . ' data-layout="' . $atts['layout'] . '" data-count="' . $atts['count'] . '" data-theme="' . $atts['theme'] . '"';
50
   if ($atts['listen'] !== false) $return .= ' data-onytevent="onYtEvent"';
51
   $return .= '></div>
52
<div style="clear: both;"></div>
53
</div>';
54
 
55
 /* Return button */
56
 return ($atts['align'] === false) ? '' . $return . '' : $return;
57
}
58
 
59
/* Usage */
60
61
 
62
/* Usage with arguments */
63
$args = array('id' => 'UC3qxAmFHsyqAnrg_UbbsHjQ', 'layout' => 'full', 'count' => 'hidden');
64

Arguments are passed to the function in the $args array.

Considerations

  • The most significant cause for confusion when using this shortcode is use of channel name and channel ID. You may only use a vanity (channel) name if YouTube approved you for such, and your channel is accessible via youtube.com/yourname. If not, use your channel ID.
  • The iframe code we've previously shared should no longer be used. While it may continue to work, it's long deprecated.
  • The listen attribute can be used with listen="1". It specifies the name of a JavaScript function that will handle event notifications related to the button. More info here .

Download


Title: YouTube Subscribe Button in WordPress (WP Plugin)
Description: Insert a YouTube subscription button with WordPress shortcode or PHP.
  Download • Version 0.2, 1.8K, zip, Category: WordPress Plugins (General)
WordPress Shortcodes, (959.0B)    PHP Code & Snippets, (1.0K)    

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

  AUS Eastern Standard Time (Connecticut)

  Want to have a chat?
 

RELATED READING

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment