In Wordpress I'm trying to build a function, which will append photo credits (including a link to the source) to images being inserted into the post (filter image_send_to_editor
). The problem is, some photos may be plain, some may be wrapped in a link tag, others might have a caption and a link too.
So I'm trying to build a preg_replace
using REGEX to look for <img>
or <a><img></a>
, so I can append my credits. I can look for all images, but then my credits with links would be placed in the image link, which is nested links -> illegal.
This is my regex so far
(<img([^>]*)><\/a>|<img([^>]*)>)
Which means my <span class="photo-creds"><a href="http://source.com">Source</a></span>
should be appended after plain image tag or after the closing link tag if the image has one.
This is a testing text, where all of the images should have photo credits appended:
<p>
Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & Javascript flavors of RegEx are supported. <img src="http://www.example.com" />CREDITS HERE</p>
The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community, and view patterns you create or favorite in My Patterns. <img src="http://www.example.com" />CREDITS HERE
Explore results with the Tools below. Replace & List output custom results. Details lists capture groups. Explain describes your expression in plain English. <a href="http://image.png"><img src="http://www.example.com" /></a>CREDITS HERE