WordPress.org

Ready to get started?Download WordPress

Forums

preg_replace to link all images in content? (2 posts)

  1. eliotc
    Member
    Posted 6 years ago #

    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.

  2. eliotc
    Member
    Posted 6 years ago #

    i ended up using a hacked up version of the Image Resizer plugin.

Topic Closed

This topic has been closed to new replies.

About this Topic