WordPress教程

纯代码WordPress站外链接自动添加nofollow和新窗口打开

阿里云

我们在写 WordPress 博客文章时经常会使用到站外链接,相信很多新手朋友也不会在意这里站外链接,但长期以往下来会造成你站点权重流失。可能有的人会说那不用外链不行呀,那因为站外链接造成权重流失这种情况怎么办呢?如何解决呢?其实方法很简单,我们只需把外链加上“nofollow”属性即可,其实目前很多主题本身应该自带这个功能。如果不知道自己的文章中的站外链接是否加有“rel=”nofollow”,请打开一个有站外链接的页面,右键查看源码,然后看外链后面是否添加了“rel=”nofollow”,如果有证明你站点的外链已做过 nofollow 处理,不会造成权重流失。如果没有,博主将在这里为大家分享一个如何防止自己的 WordPress 博客因站外链接造成权重流失,依旧是纯代码实现 WordPress 文章的站外链接自动添加 nofollow 属性和在新窗口打开。

方法一

我们只需在主题的 functions.php 文件添加下面代码即可,DUX 主题修改“functions-theme.php”文件,添加代码后会自动给你文章的的外链添加 rel=”dofollow”、target=”_blank”属性,当然如果你有些链接已经手动添加,不会受到任何影响,代码也不会重复添加。

也想出现在这里?联系我们
创客主机
  1.     add_filter( 'the_content', 'cn_nf_url_parse');
  2.     function cn_nf_url_parse( $content ) {
  3.  
  4.     	$regexp = "]*href=(\"??)([^\" >]*?)\\1[^>]*>";
  5.     	if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
  6.     		if( !empty($matches) ) {
  7.  
  8.     			$srcUrl = get_option('siteurl');
  9.     			for ($i=0; $i < count($matches); $i++)
  10.     			{
  11.  
  12.     				$tag = $matches[$i][0];
  13.     				$tag2 = $matches[$i][0];
  14.     				$url = $matches[$i][0];
  15.  
  16.     				$noFollow = '';
  17.  
  18.     				$pattern = '/target\s*=\s*"\s*_blank\s*"/';
  19.     				preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
  20.     				if( count($match) < 1 )
  21.     					$noFollow .= ' target="_blank" ';
  22.  
  23.     				$pattern = '/rel\s*=\s*"\s*[n|d]ofollow\s*"/';
  24.     				preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
  25.     				if( count($match) < 1 ) $noFollow .= ' rel="nofollow" '; $pos = strpos($url,$srcUrl); if ($pos === false) { $tag = rtrim ($tag,'>');
  26.     					$tag .= $noFollow.'>';
  27.     					$content = str_replace($tag2,$tag,$content);
  28.     				}
  29.     			}
  30.     		}
  31.     	}
  32.  
  33.     	$content = str_replace(']]>', ']]>', $content);
  34.     	return $content;
  35.  
  36.     }

方法二

第一行代码是给文章内的外链自动添加 nofollow 标签,第三方代码是给评论内的外链自动添加 nofollow 标签,这两种情况,可自行选择。如果你仅仅需要的是给文章内的外链添加 nofollow 标签,那么请删除第三行代码即可。代码添加文章同样是主题的 functions.php 内。

  1.     add_filter('the_content', 'auto_nofollow'); //nofollow文章内容的站外链接
  2.  
  3.     add_filter('comment_text', 'auto_nofollow'); //nofollow评论内容的站外链接
  4.  
  5.     function auto_nofollow($content) {
  6.      //return stripslashes(wp_rel_nofollow($content));
  7.  
  8.      return preg_replace_callback('/]+/', 'auto_nofollow_callback', $content);
  9.     }
  10.  
  11.     function auto_nofollow_callback($matches) {
  12.      $link = $matches[0];
  13.      $site_link = get_bloginfo('url');
  14.  
  15.      if (strpos($link, 'rel') === false) {
  16.      $link = preg_replace("%(href=S(?!$site_link))%i", 'rel="nofollow" $1', $link);
  17.      } elseif (preg_match("%href=S(?!$site_link)%i", $link)) {
  18.      $link = preg_replace('/rel=S(?!nofollow)S*/i', 'rel="nofollow"', $link);
  19.      }
  20.      return $link;
  21.     }

纯代码 WordPress 站外链接自动添加 nofollow 和新窗口打开

已有 330 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!