When I write a blog post on my WordPress site, I want do dynamically add a span-tag inside all the anchor tags, with a data-attribute that has the same value as the anchor tag.
Example
What I write in WordPress:
<p>Some text with <a href="#">a link in it</a></p>
What generates:
<p>Some text with <a href="#"><span data-title="a link in it">a link in it</span></a>
How can you do this with jQuery or PHP?
With PHP, you should be able to do it like that:
What this function does is that it hooks to
the_content
filter and places a span inside all anchor tags.Note that if the anchor contains an image, a span won’t be added – if you want, you can change this behaviour by changing the
_add_span
function to:A jQuery solution won’t be really difficult either, but I think that the PHP only is sufficient enough.
jQuery and wrapInner() works also:
http://jsfiddle.net/242b8/