I am looking for a way to replace all links on images. Currently when a user clicks an image it links to the source file.

What I want it to do is to ink to: example.com/editor=12&image=filename.jpg So every that encloses an should link to that site with the image filename so it can be loaded in an editor.

First, I tried to replace the links with ajax, but that does not work with lazy load and also seems less practical. The second thing I tried is to filter the $content of the post. But with that I can only replace the /wp-content/uploads/ part. I can’t filter for the first part (http://) which leads to also all src being replaced and no image shows up at all.

Is there any way to do this by hooks maybe?

Kind regards Patrick

Update: So this works for images added as single image, however it seems not to work on gallery shortcode even if the priority is 12 and short-codes are already executed.

add_filter('the_content', 'my_content_filter', 12);
function my_content_filter($content) {

if ( is_single() )

   $content = str_replace('<a href="https://example.com/wp-content/uploads/','<a href="https://example.com/?editor=82&image=',$content);
  return $content;

2 Answers 2


I've been working on a similar problem, so found a possible solution.

The issue is the 'inline' images, as opposed to images that are attachments (like a gallery), is that there is no filter for the_content() that deals specifically with the images tag. (At least, I haven't found it yet.)

So, you need to use some regex to search the_content() for the image tag, and get each image into an array, then you can loop through the image array as you need.

I found this code here: https://gist.github.com/jlord/3680879 . I have not tested it, but it may give you a start:

 // get the content from the post
  $posttext = $post->post_content;
  // next line added to process any shortcodes in the content string
  $posttext = do_shortcode($posttext);
  // make a variable for the string you're looking to find in all of the content
  $regex = '~<img [^\>]*\ />~';
  // find the first things inside of the second thing and put them inside of the third thing
  preg_match_all($regex, $posttext, $images);
  // redefine posttext as itself minus all the things you found 
  $posttext = preg_replace($regex, '', $posttext);

  // now posttext has no images, and $images is an array of image tags
  // have fun, o lord of jellies ?> <!-- this part is from issac -->

<div id="thePostText">
  // now spit out all the text
  echo $posttext; ?>

<div id='thePostImages'>
  // for each image, wrap it in the p tags and spit it out 
  foreach ( $images[0] as $image ) {
  echo '<p class="aPostImage">' . $image . '</p>'; } ?>

You would place this code in a template, inside The Loop. Then use that template for the post output.

I'd be interested if this helps. It may save you a couple of hours on the googles, which is how I found it.


Since the $posttext = $post->post_content; is not using the_content() (which also processes shortcodes), then shortcodes in the post content may not be processed. In particular, the [gallery] shortcode is not processed. I suspect other shortcodes are also not processed.

So I added an additional line to the above code:

$posttext = do_shortcode($posttext);

to get shortcodes processed in the content string.


The above code doesn't necessarily allow you to hook into the output of gallery images. So I found this code here, which can be used as a starting point: How to get post attachments in gallery post format template

if ( get_post_gallery() ) :
        $gallery = get_post_gallery( get_the_ID(), false );

        /* Loop through all the image and output them one by one */
        foreach( $gallery['src'] as $src ) : ?>
          <li> <img src="<?php echo $src; ?>" class="gallery-slider" alt="Gallery image" /> </li>

Note that all of the above are code snippets, not an end-to-end solution. But they may be helpful to put together a solution that meets your needs.

  • Hey Rick, thank you very much, that actually could help a lot! :) I will play around with this a little and will report back in a few days :)
    – Joschi
    Sep 14, 2018 at 20:30
  • Unfortunately this does not work for me, it has the same effect as $content = str_replace('<a href="https://example.com/wp-content/uploads/','<a href="https://example.com?editor=82&image=',$content); it does not find any strings at all, however this works (bit it will also replace urls in <img> tag: $content = str_replace('/wp-content/uploads/','<a /?editor=82&image=',$content); Kind of strange if you ask me, as if the tags are added way later...
    – Joschi
    Sep 14, 2018 at 21:53
  • I was just about to post, that it works with normal images but not with shortcodes :D let me fiddle arround with this a little, thanks!
    – Joschi
    Sep 14, 2018 at 22:21
  • do_shortcode seems not to help, i might be wrong, but it does not seem to execute the shortcodes but filter them out -> returns (string) Content with shortcodes filtered out. edit ok, it actually should do the shortcoes... but thats also the case if i give it the priority 12 because the shortcodes run at 11... i dont get i O_o
    – Joschi
    Sep 14, 2018 at 22:41
  • For images as attachments, try the code here as a starting point: wordpress.stackexchange.com/questions/63980/… . Sep 15, 2018 at 0:00

I dont know why, but this works for gallery as well :D @Rick Hellewell, thank you very much for your help on this topic!

add_filter('the_content', 'my_content_filter', 12);

function my_content_filter($content) {

    global $post;

    if ( is_single() ) {

        // For gallery
        $content = str_replace("<a href='https://example.com/wp-content/uploads/","<a href='https://example.com/?editor=82&image=",$content);

        $content = str_replace('<a href="https://example.com//wp-content/uploads/','<a href="https://example.com/?editor=82&image=',$content);


    return $content;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.