WordPress外部链接自动加nofollow标签函数分享

释放双眼,带上耳机,听听看~!

分享一篇WordPress外部链接自动加nofollow标签函数,可以减少网站传递出去权重,利于网站优化。

程序字段判断添加,无需手工每个链接加入,无插件添加。

代码分享

使用方法:添加到 Funcions.php 文件中。

//文章自动nofollow
add_filter( 'the_content', 'tin_seo_wl');
function tin_seo_wl( $content ) {
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>";
if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
if( !empty($matches) ) {

$srcUrl = get_option('siteurl');
for ($i=0; $i < count($matches); $i++)
{

$tag = $matches[$i][0];
$tag2 = $matches[$i][0];
$url = $matches[$i][0];

$noFollow = '';
$pattern = '/target\s*=\s*"\s*_blank\s*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$noFollow .= ' target="_blank" ';

$pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$noFollow .= ' rel="nofollow" ';

$pos = strpos($url,$srcUrl);
if ($pos === false) {
$tag = rtrim ($tag,'>');
$tag .= $noFollow.'>';
$content = str_replace($tag2,$tag,$content);
}
}
}
}

$content = str_replace(']]>', ']]>', $content);
return $content;
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
七七八八

WordPress禁止用户访问sitemap,只允许搜索引擎蜘蛛访问方法

2024-4-6 3:21:07

七七八八

wordpress定时采集发布知乎《每天60秒》

2024-4-6 3:27:12

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索