服务器测评网
我们一直在努力

WordPress 新窗口打开文章/站外链接-添加nofollow属性

网站外链接的SEO仍然更重要。今天,自动添加no追随者属性和新窗口的方法来打开WordPress文章/页面的站点外链接,并在鱼源代码的网络共享下进行。

直接安装Nofollow for external link插件,或者为当前主题将以下代码添加到Functions.php文件中:

add_filter( 'the_content', 'cn_nf_url_parse');

function cn_nf_url_parse( $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;

}

最终效果:自动向文章/页面的网站外链接添加nolow属性(rel=”nofollow”),并在新窗口中打开它们(target=”_black”属性)。如果手动向链接添加了rel=”dofollow”,则不会添加rel=”nolow”;如果手动添加了target=”_space”,则不会重复它。

有关建议:

自动向指向WordPress文章或评论的站点外链接添加NOWLOW属性

WordPress为指向指定类别的所有链接添加no追随者属性

打开新窗口中WordPress文章中的所有链接

赞(0) 打赏
未经允许不得转载:好主机测评网 » WordPress 新窗口打开文章/站外链接-添加nofollow属性

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫