Skip to content

Instantly share code, notes, and snippets.

@tigredanky
Created June 4, 2021 22:44
Show Gist options
  • Save tigredanky/a9532434f7597c9774462af37bc24f0f to your computer and use it in GitHub Desktop.
Save tigredanky/a9532434f7597c9774462af37bc24f0f to your computer and use it in GitHub Desktop.
WordPress Functions.php | Button Appears Via Timer
<?php
add_shortcode('lesson_button', function($attr) {
$timer = isset($attr['timer']) ? $attr['timer'] : 140;
ob_start();
?>
<script src="https://player.vimeo.com/api/player.js"></script>
<script>
var player;
var iframe;
var video_title;
jQuery(function(){
setTimeout(function(){
iframe = jQuery('iframe[src*="https://player.vimeo.com"]:visible').eq(0);
player = new Vimeo.Player(iframe);
player.getVideoTitle().then(function(title) {
video_title = title.toLowerCase().replace(/[^\w ]+/g,'').replace(/ +/g,'-');
// CHECK COOKIE
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(video_title + "=") == 0) {
jQuery('.lesson_button').show()
setTimeout(function(){
jQuery('.lesson_button').addClass('active');
}, 10);
} else {
var check_time = setInterval(function(){
player.getCurrentTime().then(function(seconds) {
if (seconds >= <?php echo $timer; ?>) {
jQuery('.lesson_button').show()
setTimeout(function(){
jQuery('.lesson_button').addClass('active');
}, 10);
clearInterval(check_time);
var date = new Date();
date.setTime(date.getTime() + (30 * 24 * 60 * 60 * 1000));
document.cookie = video_title + "=1" + "; expires=" + date.toUTCString() + "; path=/";
}
})
}, 1000);
}
}
});
}, 2000);
})
</script>
<style>
body div .lesson_button {
display: none;
opacity: 0;
/*transform: scaleY(0.0);*/
transition: all .5s;
}
body div .lesson_button.active {
display: block;
opacity: 1;
/*transform: scaleY(1.0);*/
}
</style>
<?php
return ob_get_clean();
});
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment