Skip to content

Instantly share code, notes, and snippets.

@oritromax
Created January 10, 2014 23:42
Show Gist options
  • Save oritromax/8364830 to your computer and use it in GitHub Desktop.
Save oritromax/8364830 to your computer and use it in GitHub Desktop.
A simple shortcode to embed responsive youtube video in wordpress. Explained in the code !
<?php
/* Copy and Paste the Whole Code Below this line */
/* When you are using it inside functions.php, You might wanna loose the start and end PHP tag */
/* The Shortcode Format Will be -> [youtube-vid id="Video ID" res="Video Resolution"] */
/* In http://www.youtube.com/watch?v=a8ZeqZrLxpw <- this video, a8ZeqZrLxpw is the id */
/* This simple Shortcode Cover 4 premade resolution and 1 fallback. 240p, 360p, 480p, 720p and 240p fallback */
/* A Very Simple Effort to make your life easier, By Oritro Ahmed [ http://ioritro.com ] */
/* And FYI: you don't need to copy this long comment section */
function clrs_video( $atts ) {
extract( shortcode_atts( array (
'id' => '',
'res' => ''
), $atts ) );
if ($res == '240') {
return '<div class="clrs-video"><iframe src="//www.youtube.com/embed/' . $id . '" height="240" width="320" allowfullscreen="" frameborder="0"></iframe></div>
';
} elseif ($res == '360') {
return '<div class="clrs-video"><iframe src="//www.youtube.com/embed/' . $id . '" height="360" width="420" allowfullscreen="" frameborder="0"></iframe></div>
';
} elseif ($res == '480') {
return '<div class="clrs-video"><iframe src="//www.youtube.com/embed/' . $id . '" height="480" width="640" allowfullscreen="" frameborder="0"></iframe></div>
';
} elseif ($res == '720') {
return '<div class="clrs-video"><iframe src="//www.youtube.com/embed/' . $id . '" height="720" width="960" allowfullscreen="" frameborder="0"></iframe></div>
';
} else {
return '<div class="clrs-video"><iframe src="//www.youtube.com/embed/' . $id . '" height="240" width="320" allowfullscreen="" frameborder="0"></iframe></div>
';
}
}
add_shortcode ('youtube-vid', 'clrs_video' );
add_action( 'wp_head', 'clrs_video_style');
function clrs_video_style () {
?>
<style>
.clrs-video {
position: relative;
padding-bottom: 56.25%;
padding-top: 30px;
height: 0;
overflow: hidden;
}
.clrs-video iframe {
position: absolute;
top:0;
left: 0;
width: 100%;
height: 100%;
}
</style>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment