Skip to content

Instantly share code, notes, and snippets.

@blakewilson
Last active March 22, 2018 22:29
Show Gist options
  • Save blakewilson/f48d97f8e188341c16189ed8fa04603d to your computer and use it in GitHub Desktop.
Save blakewilson/f48d97f8e188341c16189ed8fa04603d to your computer and use it in GitHub Desktop.
Random YouTube Video Backgrounds with Video Background Pro
<?php
/**
* Randomize a list of YouTube videos for a video background through Video Background Pro
*
* @var Array $youtube_url_list an array of YouTube URLs
* @var String $url The random YouTube URL
* @var String $shortcode The [vidbg] shortcode. Make sure to use $url for the $youtube_url
*
* @link https://pushlabs.co/docs/video-background-pro/#using-the-shortcode
*
* @author Push Labs, 02/27/18
*/
function themeprefix_vidbgpro_random_video_background() {
// Conditionally call the function only if it is a certain page
/*
if ( !is_page( PAGE_ID ) ) {
return;
}
*/
// An array of YouTube URLs
$youtube_url_list = array(
'https://youtube.com/link1',
'https://youtube.com/link2',
'https://youtube.com/link3',
);
// Use $url in the shortcode, it is the random link pulled from the array
$random_link = array_rand( $youtube_url_list );
$url = $youtube_url_list[$random_link];
// The [vidbg] shortcode. More here: https://pushlabs.co/docs/video-background-pro/#using-the-shortcode
$shortcode = '[vidbg container="#content" type="youtube" youtube_url="' . $url . '"]';
echo do_shortcode( $shortcode );
}
add_action( 'wp_footer', 'themeprefix_vidbgpro_random_video_background' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment