Reputation: 409
I need to accept a Youtube Channel ID OR Youtube username as input, and create a subscribe button for it.
Google has just changed the syntax, so that if it's a channel ID, you use this:
<div class="g-ytsubscribe" data-channelid="UC_x5XG1OV2P6uZZ5FSM9Ttw" data-layout="default" data-count="default"></div>
But if it's a channel name you omit the id
from the attribute. https://developers.google.com/youtube/youtube_subscribe_button
You must specify a value for one of the following two attributes:
data-channel
– The name of the channel associated with the button. Sample value: GoogleDevelopers.data-channelid
– The channel ID associated with the button. Sample value: UC_x5XG1OV2P6uZZ5FSM9Ttw
What would be an easy way to tell if it's a channel ID or username?
All channels I have seen start with UC but that doesn't seem to be definitive: YouTube channel and playlist ID prefixes
Who knows why they did this but it's breaking many subscribe buttons as a result :(
Upvotes: 4
Views: 2125
Reputation: 4185
Just check if your input field starts with "UC_", "HC_", etc. for a channel per your link above. If not then it's a username.
Upvotes: 1