Site logo

Archived topic

Is there a way to adjust the output of WP anchor tags?

3 replies · Started by Chris on December 10, 2022

Viewing posts 1–4 of 4

This is going to sound a little odd :)

I'm looking for a way to add a span tag just before the ending tag for every link created in WP.

So I want to go from:

<a href="https://test.com">anchor text</a>

to:

<a href="https://test.com">anchor text<span id="ext"></span></a>

I'll be targeting that span tag id with some specific jquery and css I'm working on (getting help on), but I need a way to add that span tag just before the end anchor tag and thought y'all might have a thought on how I could do that with some GeneratePress magic :)

Chris

Hi there,

unfortunately our magic doesn't extend that far :) but there is the_content filter hook you can use.

https://developer.wordpress.org/reference/hooks/the_content/

Try this snippet, it should replace any closing </a> within the content with <span id="ext"></span></a>


function closing_anchor_replace($string) {
    $pattern = '/<\/a>/';
    $replacement = '<span id="ext"></span></a>';
    return preg_replace($pattern, $replacement, $string);
}

add_filter('the_content', 'closing_anchor_replace');

I am not sure if you want the id attribute to be dynamic, as thats a whole different challenge.

Thank you, id does not need to be dynamic, so all is well - thanks again!

Chris

Glad to be of help

This archived topic is closed to new replies.