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

LowEND: $7/month/2 cores/2G/60G/4T/1 IP/KVM

LowEND is a newly established American hosting co...

drserver: $20/year/512MB memory/100GB space/512GB bandwidth/Xen/Dallas

drserver, a stable merchant. This time, we have a...

OsoLabs: $7.65/month/1GB RAM/10GB SSD space/1TB bandwidth/KVM/Los Angeles

OsoLabs, an American business, was founded in 200...

SSDapp: €9/month/6GB RAM/210GB storage/unlimited traffic/KVM/Canada

SSDapp is a newly established hosting provider th...

WINNERvps: $4.99/month/512MB memory/10GB SSD/500GB traffic/4 IP/Xen/Phoenix

WINNERvps, which has been introduced before, is a...

Kvmla: 18 yuan/month/256MB memory/15GB space/300GB traffic/4 IP/XEN

Kvmla, the last 10 IP, 1GB memory activity was pr...

123systems: $4.5/year/256MB/10GB space/500GB traffic/OpenVZ/Los Angeles

The temptation of 123systems is here again, this ...

cloudatcost: $4.8/year/512MB/10GB SSD/500GB bandwidth/KVM/Canada

cloudatcost is a Canadian hosting provider owned ...

DeinServerHost: €10/month/10GB RAM/50GB SSD space/5TB traffic/DDOS/KVM/Germany

DeinServerHost, a German hosting provider, was es...