WordPress corepress主题自动为文章标签添加该标签的链接,给外部链接加上跳转

/**
 * WordPress 自动为文章标签添加该标签的链接
 * https://www.wpdaxue.com/auto-add-tag-link.html
 */
function wpkj_auto_add_tag_link($content){

    $limit = 5; // 设置同一个标签添加几次链接

    $posttags = get_the_tags();

    if ($posttags) {
        foreach($posttags as $tag) {
            $link = get_tag_link($tag->term_id);
            $keyword = $tag->name;

            $cleankeyword = stripslashes($keyword);
            $url = '<a target="_blank" href="'.$link.'" title="'.str_replace('%s', addcslashes($cleankeyword, '$'), __('View all posts in %s')).'">'.addcslashes($cleankeyword, '$').'</a>';
            $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s';
            $content = preg_replace($regEx,$url,$content,$limit);
        }
    }

    return $content;
}
add_filter( 'the_content', 'wpkj_auto_add_tag_link', 1 );

//给外部链接加上跳转
add_filter('the_content','the_content_nofollow',999);
function the_content_nofollow($content)
{
preg_match_all('/<a(.*?)href="(.*?)"(.*?)>/',$content,$matches);
if($matches){
foreach($matches[2] as $val){
if(strpos($val,'://')!==false && strpos($val,home_url())===false && !preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i',$val)){
$content=str_replace("href=\"$val\"", "href=\"".home_url()."/go/?url=$val\"",$content);
}
}
}
return $content;
}

 

版权声明:
作者:北忘山
文章:WordPress corepress主题自动为文章标签添加该标签的链接,给外部链接加上跳转
链接:https://www.beiwangshan.com/archives/515.html
来源:beiwangshan.com
文章版权归作者所有,未经允许请勿转载。
THE END
分享
二维码
打赏
< <上一篇
下一篇>>
文章目录
关闭
目 录