WordPress 博客外链添加 nofollow 属性避免权重流失 WP 主题优化增强 function 文件实用功能代码段

ximagine
ximagine
站长
2088
文章
1
粉丝
教程 为什么评论128字数 231阅读0分46秒阅读模式
宝藏摘要WordPress 博客经常会遇到评论的垃圾链接,需要给网站之外的链接添加 Nofollow 属性让网站权重不在流失,nofollow 是一个 HTML 标签的属性值,作用是告诉搜...
荒岛广告位招商进行中...
WordPress 博客外链添加 nofollow 属性避免权重流失 WP 主题优化增强 function 文件实用功能代码段
日期:2023年8月18日 分类:教程  为什么 评论:发表 浏览:128

WordPress 博客经常会遇到评论的垃圾链接,需要给网站之外的链接添加 Nofollow 属性让网站权重不在流失,nofollow 是一个 HTML 标签的属性值,作用是告诉搜索引擎不要追踪此网页上的链接或不要追踪此特定链接,荒岛本次带来 WordPress 博客外链添加 nofollow 属性的教程,只要将下面的代码添加到主题的「functions.php」文件即可。

// 荒岛一座藏有宝藏的小岛
add_filter( 'the_content', 'add_nofollow_content' );
function add_nofollow_content($content) {
	$content = preg_replace_callback( '/<a[^>]*href=["|\']([^"|\']*)["|\'][^>]*>([^<]*)<\/a>/i',
		function( $m ) {
			$site_link = get_option( 'siteurl' );
			$site_link_other = get_option( 'siteurl' );
			if (strpos( $m[1], $site_link  ) === false && strpos( $m[1], $site_link_other ) === false )
				return '<a href="'.$m[1].'" rel="external nofollow" target="_blank">'.$m[2].'</a>';
			else
			return '<a href="'.$m[1].'" target="_blank">'.$m[2].'</a>';
		},
		$content
	);
	return $content;
}
宝藏内容来自荒岛-https://x-imagine.com/wordpress-external-link-nofollow-attribute.html宝藏内容来自荒岛-https://x-imagine.com/wordpress-external-link-nofollow-attribute.html

请按「Ctrl+D」收藏荒岛分享给好友,如您发现本文教程或宝藏链接已失效,请联系站长修正!

荒岛公众号
扫一扫关注
weinxin
HDsoft27
公众号ID已复制,前往微信关注...
荒岛哔哩姬
扫一扫关注
weinxin
ximagine
哔哩姬ID已复制,前往B站关注...
荒岛广告位招商进行中...
ximagine
荒岛广告位招商进行中...

发表评论