I've searched for an answer to what seems like a simple question, but to no avail.
I have some HTML output, eg:
<div id="attachment_1098" class="wp-caption alignleft" style="width: 160px">[[ a href="mydomain.com/wp-content/uploads/2010/11/photo-1.jpg" ]][[img class="size-thumbnail wp-image-1098" title="John Doe" src="mydomain.com/wp-content/uploads/2010/11/photo-1.jpg" alt="John Doe" width="150" height="200" /]][[ /a ]]<p class="wp-caption-text">John Doe</p></div>
<div id="attachment_1080" class="wp-caption alignleft" style="width: 160px">[[ a href="mydomain.com/wp-content/uploads/2010/11/photo-2.jpg" ]][[img class="size-thumbnail wp-image-1080" title="Fred Bloggs" src="mydomain.com/wp-content/uploads/2010/11/photo-1.jpg" alt="Fred Bloggs" width="150" height="200" /]][[ /a ]]<p class="wp-caption-text">Fred Bloggs</p></div>
<div id="attachment_1099" class="wp-caption alignleft" style="width: 160px">[[ a href="mydomain.com/wp-content/uploads/2010/11/photo-3.jpg" ]][[img class="size-thumbnail wp-image-1099" title="Jane Doe" src="mydomain.com/wp-content/uploads/2010/11/photo-3.jpg" alt="Jane Doe" width="150" height="200" /]][[ /a ]]<p class="wp-caption-text">Jane Doe</p></div>
<div id="attachment_1096" class="wp-caption alignleft" style="width: 160px">[[ a href="mydomain.com/wp-content/uploads/2010/11/photo-4.jpg" ]][[img class="size-thumbnail wp-image-1096" title="Joe Public" src="mydomain.com/wp-content/uploads/2010/11/photo-4.jpg" alt="Joe Public" width="150" height="200" /]][[ /a ]]<p class="wp-caption-text">Joe Public</p></div>
(The above html is actually well formed, had to replace < & > in anchours and images with [[ and ]] respectively so it would post.)
I'd like to wrap a link around the text content of each (p) with a class of "wp-caption-text", the url of which should have the following format:
'/my-page-root/'.strtolower(str_replace(' ','-',$linkText)).'';
(so John Doe would link to: /my-page-root/john-doe etc)
Now, I've got so far with preg_replace, but I can't figure out how to create the link URL from the backreference. This makes me think there must be an easier way to do it (and possibly a quicker way?)
Anyway, here's the code I got up to:
<?php $contentLinked = preg_replace("/(<p class=\"wp-caption-text\">.*<\/p>)/Us", "<p class=\"wp-caption-text\"><a href=\"\">$1</a></p>",$content); ?>
That replaces the text with an empty anchour just fine, I'm stuck with how to use the backreference to create the link url.
A few Notes:
The link must only wrap the text (may need to only wrap part of the text later on, but I'll cross that bridge when I come to it)
Wordpress won't allow this through the wysiwyg without a workaround to prevent stripping the links, one day someone else might come in and wreck the format (hence the hard coding) so there are no useful wp workarounds for this.
The link on the image must remain to the full sized image (jquery gallery going on), so wrapping the entire image and/or div in the link is not an option
Cheers for any help you can give me.
Andy