• Hello, I’m trying make a short plugin to replace all <img> tags in the_content with <href><img> tags. Is this possible with preg_replace?

    More specifically, i’d like to place every:
    <img src="anyfile">
    With
    <a href="anyfile"><img src="anyfile"></a>

    I’m not experienced with regex, but here is what i have so far, which isn’t working:

    <?php
    define("IMAGE_FILETYPE", "(bmp|gif|jpeg|jpg|png)", true);
    
    function everyimage_create($string){
    	$pattern = '/<a(.*?)img(.*?)src="(.*?).(bmp|gif|jpeg|jpg|png)"(.*?)>/i';
      	$replacement = '<a$1$2href="$3.$4" rel=\'lightbox[imagegroup]\'$5><a img src=$3.$4"></a>';
    	return preg_replace($pattern, $replacement, $string);
    }
    
    add_filter('the_content', 'everyimage_create');
    
    ?>

    The overall goal is to have embedded images be clickable, so that they open in their original size using lightbox (or slimbox, etc.)

    Thanks for any help or tips.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘preg_replace to link all images in content?’ is closed to new replies.