Tutorial – How To Change The WordPress “Read More” Anchor in Your Post

I recently had come across a challenge in this very WordPress site. I needed to change the “Read More” anchor tag in my posts so that I could leverage certain functionality I wanted to implement. The standard “Read More” anchor looks something like this <span id="more-{id}"></span>. I wanted to add some content inside of the <span> tag. Here’s how I did it.

function update_more_anchor($content) {
    $insertContent = '<!--{{CONTENT HERE}}-->';
    return preg_replace('/<span id\=\"(more\-\d+)"><\/span>/', '<span id="\1">' . $insertContent . '</span>', $content);
}
add_filter('the_content', 'update_more_anchor');

This method uses a regex to find and replace the <span> tag within the content itself. This results in and outcome of <span id="more-{id}"><!--{{CONTENT HERE}}--></span>.

Hope this helps!

Leave a comment

Leave a Reply