Crave: A modern WordPress theme with appetizing aesthetic


Results 1 to 14 of 14

Thread: Add a YouTube Channel Subscription button with a WordPress Shortcode

  1. #1
    Join Date
    Jan 2014
    Location
    Tardis
    Posts
    661

    Lightbulb Add a YouTube Channel Subscription button with a WordPress Shortcode

    Hey guys,

    I was watching one of Lisa's videos recently and she mentioned about placing a Subscribe button next to your embedded YouTube videos. Her logic made complete sense. So I did it.

    Then I got to thinking. Wow.....cutting and pasting this code is going to be annoying.

    So I created a WordPress ShortCode called [youtubefollow] and now I can type that into any of my content (I stick it right above the video actually) and a Subscribe button will show up for my channel.

    I made it all lowercase because it is faster to type that way. Secondly, I can't use [youtube] as that shortcode already exists.

    Here is the code for my shortcode.

    function SubscribeToYouTubeChannel() {
    return '<script src="https://apis.google.com/js/platform.js"></script>
    <div class="g-ytsubscribe" data-channelid="UCOcNnCrrEkYl3zXssQ3-7Bg" data-layout="default" data-count="default"></div>';
    }
    add_shortcode('youtubefollow', 'SubscribeToYouTubeChannel');

    Now the parameter data-channelid is the ID of My YouTube Channel, so you will need to get yours and cut and replace as needed. You can also use the EXACT name of your channel here as well. I screwed this up (there is another SuperTekBoy on YouTube - no videos either--- GRR) so I used the ID instead - no errors that way.

    Just cut and paste this code into your shortcodes.php file which can be found under your wp-includes folder in your WordPress installation path.

    Hope this helps.

    1-12-2014 3-59-35 PM-B.jpg
    Last edited by STB; 01-12-2014 at 03:08 PM.

  2. #2
    Join Date
    Apr 2007
    Posts
    14,905

    Default

    Now that's what I call making use of a shortcode! This is invaluable! Thanks for all the details.

    Just a small request, in the future if you share something you've done can you take a screenshot so we can see the button instead of linking to your site? I don't want to start a trend of "See what I did" posts. We have enough link drop spam to deal with it as it is. Yours was harmless, but it's about setting the precedent so we don't give spammers any ideas. Hope you understand.

  3. #3
    Join Date
    Jan 2014
    Location
    Tardis
    Posts
    661

    Default

    Fixed.

  4. #4
    Join Date
    Sep 2012
    Location
    Ilkeston, Derbyshire, England
    Posts
    1,595

    Default

    Thanks for this, it'll be handy for a WP site I'm building soon. I have another blog that is not WordPress so I used the following guide which is similar in principle to what you did:

    https://developers.google.com/youtub...bscribe_button
    Last edited by Darren; 01-12-2014 at 04:29 PM.

  5. #5
    Join Date
    Apr 2007
    Posts
    14,905

    Default

    Appreciate it. I was going to let that one stay but thanks for understanding.

  6. #6
    Join Date
    Sep 2012
    Location
    Ilkeston, Derbyshire, England
    Posts
    1,595

    Default

    just to clarify, you are instructing us to put the code in shortcodes.php? All the WP tutorials I have read only talk about functions.php..

  7. #7
    Join Date
    Jan 2014
    Location
    Tardis
    Posts
    661

    Default

    You can place it directly in functions.php. But if you plan to create a lot of your own shortcodes most tutorials recommend you create a separate linked file. It can also help reduce the risk of messing something up in your functions file.

    I don't how long it has been around but shortcodes.php is now a file in WordPress that contains the short codes. So I just drop my short codes in there instead. You could create another file called myshortcodes.php and link it back to functions.php as an include.

  8. #8
    Join Date
    Sep 2012
    Location
    Ilkeston, Derbyshire, England
    Posts
    1,595

  9. #9
    Join Date
    Jan 2014
    Location
    Tardis
    Posts
    661

    Default

    I stick mine right at the end. After everything else.

  10. #10
    Join Date
    Sep 2012
    Location
    Ilkeston, Derbyshire, England
    Posts
    1,595

  11. #11
    Join Date
    Jan 2014
    Location
    Tardis
    Posts
    661

    Default

    Hmm.... I'm on 3.8 as well. In case that makes a difference.

  12. #12
    Join Date
    Sep 2012
    Location
    Ilkeston, Derbyshire, England
    Posts
    1,595

    Default

    Not sure at present, I don't need to use shortcodes for a while but I'll need to revisit this in more depth soon. The way you describe adding the code to shortcodes.php is not something I can find anywhere online - the tutorials people do always seems to be about functions.php. This is therefore probably a good topic for a video to put on your site. If you do this, let me know!

  13. #13
    Join Date
    Aug 2009
    Location
    Wisconsin
    Posts
    729

    Default

    Awesome informative post!

    I have been working on Wordpress customizations with code and not plugins for designing responsive websites for clients. I'll have to add this in as well.

    My main advice is that you should -always- create a child theme though and place your code in it. Otherwise when wordpress updates, you could lose your custom scripts.
    Nice job
    Like Bill at Facebook and Twitter.

    and now Bill's Blog.

  14. #14
    Join Date
    Sep 2012
    Location
    Ilkeston, Derbyshire, England
    Posts
    1,595

    Default

    i think my theme must be preventing some of the functionality of this... the code in shortcodes.php is not working because when I enter the shortcode [youtubefollow] that's exactly what I get in the text widget. I'll just have to stick with pasting the entire YouTube code into whereever I want the button to show up

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •