I'm trying to add a filter to the content to replace the src of all images in the content with it's surrounding href link. Any ideas how to go about this?

The only thread I could find that came close was this: Replace image attributes for lazyload plugin (data-src)

  • This sounds like something you'd want for printing a page - to pick the highest-res version of an image - so maybe there's a printing plugin that'll do this for you. Doesn't the answer you've linked to help - that shows you how to parse an HTML page into a DOM so you can traverse it and e.g. find all images, then check their parents, then copy the URL if it's an image, etc.?
    – Rup
    Mar 9, 2015 at 10:15

1 Answer 1


Use this for example in the loop

$new_url = '<img src="http://www.google.nl';
$content = apply_filters('the_content', get_the_content());
$content = preg_replace("/(<img[^>]*src *= *[\"']?)([^\"']*)/i", $new_url,  $content);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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