Automatically adding internal links in the content of WordPress articles, such as references to article tags. This not only improves the SEO effect of the website, but also enhances the user experience, making it easier for users to find related content on your website. In fact, there are many articles about automatically adding tag internal links to WordPress articles, and almost all of them use the same piece of code. I have also been using it, and today I just wanted to optimize the internal linking of the website, so I decided to rewrite the function of automatic internal linking.
The code is actually very simple, but it may look a bit long. This version of the code will replace the text inside the img, a, and code tags with a special marker "%&&&&&%", and then replace the text of the tags, and finally replace the special marker back to the original tag text. This avoids replacing the tag text within these tags. Without further ado, here is the code:
//WordPress article tag automatic internal linking
add_filter('the_content', 'fanly_auto_tags_link');
function fanly_auto_tags_link($content) {
$tags = get_the_tags(); //Get the tags of the current article
if($tags){
foreach ($tags as $tag) {
$link = get_tag_link($tag->term_id); //Generate the tag link
$tag_name = preg_quote($tag->name, '/'); //Escape the tag name
//Protect the content inside <a>, <img>, and <code> tags in advance
$content = preg_replace_callback('/(<a[^>]*>)(.*?)(<\/a>)/si', function($matches) use ($tag_name) {
return str_replace($tag_name, '%&&&&&%', $matches[0]);
}, $content);
$content = preg_replace_callback('/(<img[^>]*)(.*?)(' . $tag_name . ')(.*?)(>)/si', function($matches) use ($tag_name) {
return str_replace($tag_name, '%&&&&&%', $matches[0]);
}, $content);
$content = preg_replace_callback('/(<code[^>]*>)(.*?)(<\/code>)/si', function($matches) use ($tag_name) {
return str_replace($tag_name, '%&&&&&%', $matches[0]);
}, $content);
//Replace the tag text in the content with a link, limiting the replacement to 1 time
$content = preg_replace('/' . $tag_name . '/iu', '<a href="' . $link . '">' . $tag->name . '</a>', $content, 1);
//Restore the content inside the protected tags
$content = str_replace('%&&&&&%', $tag_name, $content);
}
}
return $content;
}
This code will automatically create a link for each tag in the content of the article. First, it gets all the tags of the current article, then for each tag, it generates a link to the tag archive page. Then, it uses regular expressions to find the tag text in the content and replace it with a link. Here, we use the preg_replace_callback function to avoid replacing the content inside HTML tags.
It looks much more standardized now. In fact, this is just a simple usage and the laziest way to optimize internal linking on a website. However, since everyone's usage scenarios are different, I provide a basic solution for reference. In fact, you can enhance it or even add custom links.
For more information and questions about WordPress optimization, you can join the QQ group: 255308000
Unless otherwise stated, all articles are original articles from the Tearsnow Blog, and any form of reproduction is prohibited.
Article link: https://zhangzifan.com/wordpress-auto-tags-link.html