I had previously used a bit of code I found to add "rel = shadowbox" to my links in Wordpress (Which works like a charm).
/*
Plugin Name: Add shadowbox
*/
define("IMAGE_FILETYPE", "(bmp|gif|jpeg|jpg|png)", true);
function addlightboxrel_replace($string) {
$pattern = '/<a(.*?)href="(.*?).(bmp|gif|jpeg|jpg|png)"(.*?)>/i';
$replacement = '<a$1href="$2.$3" rel='shadowbox'$4>';
return preg_replace($pattern, $replacement, $string);
}
add_filter('the_content', 'addlightboxrel_replace');
I had to use a separate plugin to add some gallery images. This plugin however didn't wrap the thumbnail image in an "a" tag (and there's no option to do such). So rather than edit the plugin, I am trying to use the same replace idea to add a link around the image.
I don't quite understand the first replace code, so I'm sure I'm missing something. I'm also trying to replace more than one line of code, so I'm not sure if that's where it's breaking. This is what I have:
define("IMAGE_FILETYPE", "(bmp|gif|jpeg|jpg|png)", true);
function addlink_replace($string) {
$pattern = '/<ul class="slides"(.*?)><li(.*?)><img(.*?)src=(.*?)><(.*?)li><(.*?)ul>/i';
$replacement = '<ul class="slides"$1><li$2><a src="$4"><img$3src=$4></a><$5li><$6ul>';
return preg_replace($pattern, $replacement, $string);
}
add_filter('the_content', 'addlink_replace');
This is the current code being spit out by the plugin:
<ul class="slides" style="width: 600%; transition-duration: 0s; transform: translate3d(0px, 0px, 0px);">
<li style="float: left; display: block; width: 100px;">
<img width="110" height="110" class="slider-247 slide-243" alt="" src="http://carerforklifts.com/wp-content/uploads/2014/04/F16H-1-110x110.jpg" draggable="false">
</li>
</ul>
And I would like to wrap a "a" tag around the image, using the current images URL for the a tag's src. (If possible I need to remove that "-110x100" bit at the end of the jpg.
<ul class="slides" style="width: 600%; transition-duration: 0s; transform: translate3d(0px, 0px, 0px);">
<li style="float: left; display: block; width: 100px;">
<a src="http://carerforklifts.com/wp-content/uploads/2014/04/F16H-1.jpg">
<img width="110" height="110" class="slider-247 slide-243" alt="" src="http://carerforklifts.com/wp-content/uploads/2014/04/F16H-1-110x110.jpg" draggable="false">
</a>
</li>
</ul>
Currently working on this site if that helps: http://www.carerforklifts.com/f16-h/
See Question&Answers more detail:
os