WordPress implements automatic article tag extraction and link addition

WordPress implements automatic article tag extraction and link addition


There are many ways to automatically extract tags from articles in WordPress, and the most convenient way is to use a plug-in. Today I will share a method to automatically extract tags and add links to articles without using plugins.
Log in to the blog backend, open the function.php function of the blog theme, and add the following code to this file:

//連接數量
$match_num_from = 1; //一個關鍵字少于多少不替換
$match_num_to = 10; //一個關鍵字最多替換
//連接到WordPress的模塊
add_filter('the_content','tag_link',1);
//按長度排序
function tag_sort($a, $b){
if ( $a->name == $b->name ) return 0;
return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
}
//改變標簽關鍵字
function tag_link($content){
global $match_num_from,$match_num_to;
$posttags = get_the_tags();
if ($posttags) {
usort($posttags, "tag_sort");
foreach($posttags as $tag) {
$link = get_tag_link($tag->term_id);
$keyword = $tag->name;
//連接代碼
$cleankeyword = stripslashes($keyword);
$url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('View all posts in %s'))."\"";
$url .= ' target="_blank"';
$url .= ">".addcslashes($cleankeyword, '$')."</a>";
$limit = rand($match_num_from,$match_num_to);
//不連接的代碼
$content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
$content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
$cleankeyword = preg_quote($cleankeyword,'\'');
$regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;
$content = preg_replace($regEx,$url,$content,$limit);
$content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
}
}
return $content;
}

Purely for your own convenience.

<<:  $19.95/month/512M memory/24G space/2TB traffic/XEN VPS —— Linode

>>:  VPS for less than $20 per year

Recommend

vmbox.co latest 50% discount, free 1 year SSL certificate

It has been introduced before: https://zhujiwiki....

Chicago VPS: $5/month/2GB memory/60GB space/2TB traffic/KVM/Los Angeles/New York

Both ChicagoVPS and HudsonValleyHost were acquire...

vps.net: $5/month/1GB RAM/30GB SSD space/2TB traffic/Xen/Salt Lake/UK

vps.net, a long-established hosting provider, pro...

9xhost: $95/month/E3 1240v5/32GB memory/4TB space/100TB traffic/Netherlands

9xhost, an American hosting provider, was founded...

SpectraIP: €5/month/1GB RAM/30GB SSD space/1TB traffic/10Gbsp/KVM/Netherlands

SpectraIP, a Dutch merchant, has its own AS (AS62...

StarryDNS: $7/month/512MB memory/20GB space/100GB traffic/OpenVZ/Korea

StarryDNS, a company in Shenzhen, Shenzhen Tinmok...

Node Wing: $39/year/1GB memory/60GB space/unlimited traffic/KVM/France

Node Wing, a foreign hosting company, was establi...

Tospeed 512MB RAM 100GB Space Novosibirsk KVM VPS Review

Details: Tospeed: 24 yuan/month/512MB memory/150G...

AuroraCloud: Chinese Valentine's Day Event

Add 520CNY and get 100CNY for free Add 1314CNY an...