Shortcode content filter?

I’m looking for something, but don’t exactly know what.

I have a shortcode, let’s call it [shortcode].

Read More

Users will input HTML tags inside, mostly images, but also links, images in links, etc., for example:

[shortcode]
<img src="http://www.site.com/myimage.jpg" />
<a href="http://www.blabla.com"><img src="http://www.site.com/myimage2.jpg" /></a>
(...)
[/shortcode]

The point is I want to format URLs, differently, I want every img src to start with files/myimagescript?

So the code above should output:

<!--- shortcode code before input -->
<img src="files/myimagescript?http://www.site.com/myimage.jpg" />
    <a href="http://www.blabla.com"><img src="files/myimagescript?http://www.site.com/myimage2.jpg" /></a>
    (...)
<!-- shortcode code after input -->

So basically I need to simply change src of images. And it should work for any number of images, from 1 to unlimited.

I’m thinking about foreach PHP loop, but I’m not sure how to grab each img src line from shortcode and process it before displaying?

Related posts

Leave a Reply

1 comment

  1. you can use regex to find your the src and use that to append your “files/myimagescript?” to it:

    function append_myimagescript($attr, $content){ 
        $pattern = '/src="([^"]*)"/i';
        $replacement = 'files/myimagescript?${1}';
        return preg_replace($pattern, $replacement, $content);
    }