Skip to content

Instantly share code, notes, and snippets.

@hirejordansmith

hirejordansmith/functions.php Secret

Created Mar 23, 2018
Embed
What would you like to do?
Youtube API WordPress Shortcode Example
<?php
add_shortcode( 'youtube', function( $attr ) {
$attr = wp_parse_args( $attr, array(
'id' => ''
) );
ob_start();
?>
<script async src="https://www.youtube.com/iframe_api"></script>
<script>
var players = new Array();
function onYouTubeIframeAPIReady() {
var playerDivs = document.getElementsByClassName('ytiframevideo');
for(var i = 0; i < playerDivs.length; i++) {
var playerid = playerDivs[i].getAttribute('id');
var vidid = playerDivs[i].getAttribute('vidid');
players[playerid] = new YT.Player(playerid, {
videoId: vidid,
width: 560,
height: 316,
playerVars: {
'version': 3,
'autoplay': 1,
'controls': 1,
'showinfo': 0,
'modestbranding': 1,
'loop': 1,
'playlist': '<?php echo $attr['id']; ?>',
'rel': 0,
'fs': 0,
'cc_load_policty': 0,
'iv_load_policy': 3
},
events: {
'onReady': function (e) {
e.target.mute();
//e.target.setVolume(50); // YOU CAN SET VALUE TO 100 FOR MAX VOLUME.
}
}
});
}
}
</script>
<div class="embed-container">
<!-- <div id="youtubediv"></div> -->
<div id="player-<?php echo $attr['id']; ?>" vidid="<?php echo $attr['id']; ?>" class="ytiframevideo"></div>
</div>
<?php
return ob_get_clean();
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.