Videos are one of the best ways to boost user engagement. Recently one of our users asked us about a way to create share button overlays on videos similar to the popular site UpWorthy. In this article, we will show you how to add share buttons as overlay on YouTube videos in WordPress.
There were several ways that this could be done. Most ways would require you to paste a bits of HTML code every time you add a video. Instead of doing that, we decided to create a shortcode that would automate this overlay effect.
Simply copy and paste the following code in a site-specific plugin or your theme’sfunctions.php file:
/// WPBeginner's YouTube Share Overlay Buttons
function wpb_yt_buttons($atts) {
// Get the video ID from shortcode
extract( shortcode_atts( array(
'video' => ''
), $atts ) );
// Display video
$string = '<div id="video-container"><iframe src="//www.youtube.com/embed/' . $video . '" height="315" width="560" allowfullscreen="" frameborder="0"></iframe>';
// Add Facebook share button
$string .= '<ul class="share-video-overlay" id="share-video-overlay"><li class="facebook" id="facebook"><a href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fyoutube.com/watch%3Fv%3D'. $video .'" target="_blank">
Facebook</a></li>';
// Add Tweet button
$string .= '<li class="twitter" id="twitter"><a href="http://www.twitter.com/share?&text=Check+this+video&url=http%3A//www.youtube.com/watch%3Fv%3D'. $video .'">Tweet</a></li></ul>';
// Close video container
$string .= '</div>';
// Return output
return $string;
}
// Add Shortcode
add_shortcode('wpb-yt', 'wpb_yt_buttons');
This code creates a shortcode that automatically adds twitter and facebook share links to your videos. These buttons are only visible when the user brings their mouse over the video. You can use it add any other social media channel as well.
To use this shortcode, all you will need to do is add the YouTube video ID in the shortcode like this:
[wpb-yt video="qzOOy1tWBCg"]
No comments:
Post a Comment