万维当云伯乐 CDN华纳云
WordPress教程2023-11-211,304 阅读

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

本文摘要网站外链接的SEO仍然更重要。今天,自动添加no追随者属性和新窗口的方法来打开WordPress文章/页面的站点外链接,并在鱼源代码的网络共享下进行。 直接安装Nofollow for external link插件,或者为当前主题将以下代码添加到Functions.php文件中: add_filter( 'the_c
丽萨主机
AD:
【广告招商】文章正文文字广告位开放合作
网站外链接的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文章中的所有链接
广告招商文章内容详情页下 · 优质席位开放中合作咨询