Site logo

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

Home Forums Support [Resolved] Is there a way to adjust the output of WP anchor tags?

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

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2457534
    Chris

    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

    #2457956
    David
    Staff
    Customer Support

    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.

    #2457984
    Chris

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

    Chris

    #2458021
    David
    Staff
    Customer Support

    Glad to be of help

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.