Created
April 3, 2012 19:05
-
-
Save dalethedeveloper/2294765 to your computer and use it in GitHub Desktop.
WordPress: Popup YouTube Videos in Thickbox (via shortcode)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add a custom shortcode to popup youtube videos in a thickbox overlay. | |
* Pastable to functions.php in a WordPress theme. | |
* | |
* Usage (in WP Editor): | |
* [ytp url="http://www.youtube.com/watch?v=xxxxxxxxxxxxx"] | |
* | |
* From: http://manchumahara.com/2010/03/22/using-wordpress-native-thickbox/ | |
*/ | |
// Handle the [ytp] shortcode | |
function do_ytp($att) { | |
if( isset($att['url']) ) { | |
$id = 'ytp'.rand(); | |
$oe = json_decode( file_get_contents('http://www.youtube.com/oembed?url='.urlencode($att['url']) ) ); | |
$href = "#TB_inline?height={$oe->height}&width={$oe->width}&inlineId={$id}"; | |
return <<<HTML | |
<div id="{$id}" style="display:none;">{$oe->html}</div> | |
<h3><a class="thickbox" href="{$href}">{$oe->title}</a></h3> | |
<a class="thickbox" href="{$href}"> | |
<img src="{$oe->thumbnail_url}"/> | |
</a> | |
HTML; | |
} | |
} | |
add_shortcode('ytp','do_ytp'); | |
// Add Thickbox to our frontend scripts | |
function add_ytp(){ | |
if(!is_admin()){ | |
wp_enqueue_script('jquery'); | |
wp_enqueue_script('thickbox',null,array('jquery')); | |
wp_enqueue_style('thickbox.css', '/'.WPINC.'/js/thickbox/thickbox.css'); | |
} | |
} | |
add_action('init','add_ytp'); | |
// Add some global JS variables thickbox requires | |
function head_ytp() { | |
$url = get_bloginfo('url'); | |
echo <<<HTML | |
<script type="text/javascript"> | |
if ( typeof tb_pathToImage != 'string' ) | |
var tb_pathToImage = "{$url}/wp-includes/js/thickbox/loadingAnimation.gif"; | |
if ( typeof tb_closeImage != 'string' ) | |
var tb_closeImage = "{$url}/wp-includes/js/thickbox/tb-close.png"; | |
</script> | |
HTML; | |
} | |
add_action('wp_head', 'head_ytp'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment