Howto use WP built in Thickbox for images?
I've been searching for a better way to utilize the built in Thickbox function for images. I've been using my method for a few months, but I don't think it's the best way to do it. Here is the code I've been using (I don't remember where I found the code or I'd link to the article):
?php
if (!is_admin()){
/*
add a word found in your domain name
*/
$mydomain = ".com";
/*
The css selector
.entry-content a img
*/
$myselector = "p a img";
wp_enqueue_style('thickbox'); //include thickbox .css
wp_enqueue_script('jquery'); //include jQuery
wp_enqueue_script('thickbox'); //include Thickbox jQuery plugin
// Function that will write js
function thickbox_js(){
global $mydomain, $myselector;
?
script type='text/javascript'
var tb_closeImage = "?php bloginfo('wpurl'); ?/wp-includes/js/thickbox/tb-close.png";
var tb_pathToImage = "?php bloginfo('wpurl'); ?/wp-includes/js/thickbox/loadingAnimation.gif";
jQuery(document).ready(function() {
jQuery("?php echo $myselector; ?").parent("a[href*=?php echo $mydomain; ?]").addClass("thickbox");
});
/script
?php
}
add_action('wp_footer', 'thickbox_js'); // use wp_footer hook to write our generated javascript into page footer.
}
?
Any suggestions would be appreciated. I've tried several different methods listed in tutorials, but none have worked. Thanks...
To be more specific, this is the part in question:
?php bloginfo('wpurl'); ?
What I'm using now:
?php
function add_themescript(){
if(!is_admin()){
wp_enqueue_script('thickbox',null,array('jquery'));
wp_enqueue_style('thickbox.css', '/'.WPINC.'/js/thickbox/thickbox.css', null, '1.0');
}
}
add_action('init','add_themescript');
define("IMAGE_FILETYPE", "(bmp|gif|jpeg|jpg|png)", true);
function wp_thickbox($string) {
$pattern = '/(a(.*?)href="([^"]*.)'.IMAGE_FILETYPE.'"(.*?)img)/ie';
$replacement = 'stripslashes(strstr("\2\5","rel=") ? "\1" : "a\2href=\"\3\4\"\5 class=\"thickbox\"img")';
return preg_replace($pattern, $replacement, $string);
}
function wp_thickbox_rel( $attachment_link ) {
$attachment_link = str_replace( 'a href' , 'a rel="thickbox-gallery" class="thickbox" href' , $attachment_link );
return $attachment_link;
}
add_filter('the_content', 'wp_thickbox');
add_filter('wp_get_attachment_link' , 'wp_thickbox_rel');
?
I thinks that's a little better, but I'm still not sure it's the optimal way. Next I need to learn how to add navigation to the ThickBox pop-up images.
Topic thickbox images Wordpress javascript
Category Web