• 欢迎访问老K博客,关注网络营销技术,分享网络营销精神!欢迎加入我们
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏老K博客吧

WordPress外链自动新窗口打开并添加nofollow属性

WordPress教程 老K 9个月前 (11-20) 384次浏览 已收录 0个评论

今天群里有人问我如何在WordPress站点上,给外链自动添加nofollow属性。因为每次设置太麻烦,所以我的博客很早之前就弄了,今天有人提起,就分享给大家!

WordPress外链自动新窗口打开并添加nofollow属性

两种方法均为修改 functions.php 文件。

方法一

<span class="com">/**
 * </span><span class="com">WordPress外链</span><span class="com">自动新窗口打开并添加<a href="http://www.laoking.net/tag/nofollow%e5%b1%9e%e6%80%a7" title="查看更多关于nofollow属性的文章" target="_blank">nofollow属性</a> - 方法一
 * 
 */</span>
<span class="kwd">function</span><span class="pln"> cleris_url</span><span class="pun">(</span><span class="pln">$atts</span><span class="pun">,</span><span class="pln"> $url </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">){</span><span class="pln">
	extract</span><span class="pun">(</span><span class="pln"> shortcode_atts</span><span class="pun">(</span><span class="pln">array</span><span class="pun">(</span><span class="str">'title'</span> <span class="pun">=></span> <span class="kwd">null</span><span class="pun">,</span> <span class="str">'href'</span> <span class="pun">=></span> <span class="kwd">null</span><span class="pun">),</span><span class="pln"> $atts</span><span class="pun">)</span> <span class="pun">);</span>
	<span class="kwd">return</span> <span class="str">'<span class="u-download"><a target="_blank" title="'</span><span class="pun">.</span><span class="pln">$title</span><span class="pun">.</span><span class="str">'" href="'</span><span class="pun">.</span><span class="pln">$href</span><span class="pun">.</span><span class="str">'" rel="external <a href="http://www.laoking.net/tag/nofollow" title="查看更多关于nofollow的文章" target="_blank">nofollow</a>" target="_blank">'</span><span class="pun">.</span><span class="pln">$url</span><span class="pun">.</span><span class="str">'</a></span>'</span><span class="pun">;</span>
<span class="pun">}</span>

方法二

<span class="com">/**
 * WordPress外链自动新窗口打开并添加<a href="http://www.laoking.net/tag/nofollow%e5%b1%9e%e6%80%a7" title="查看更多关于nofollow属性的文章" target="_blank">nofollow属性</a> - 方法二
 * 
 */</span><span class="pln">
add_filter</span><span class="pun">(</span> <span class="str">'the_content'</span><span class="pun">,</span> <span class="str">'cn_nf_url_parse'</span><span class="pun">);</span>
<span class="kwd">function</span><span class="pln"> cn_nf_url_parse</span><span class="pun">(</span><span class="pln"> $content </span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
	$regexp </span><span class="pun">=</span> <span class="str">"<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>"</span><span class="pun">;</span>
	<span class="kwd">if</span><span class="pun">(</span><span class="pln">preg_match_all</span><span class="pun">(</span><span class="str">"/$regexp/siU"</span><span class="pun">,</span><span class="pln"> $content</span><span class="pun">,</span><span class="pln"> $matches</span><span class="pun">,</span><span class="pln"> PREG_SET_ORDER</span><span class="pun">))</span> <span class="pun">{</span>
		<span class="kwd">if</span><span class="pun">(</span> <span class="pun">!</span><span class="pln">empty</span><span class="pun">(</span><span class="pln">$matches</span><span class="pun">)</span> <span class="pun">)</span> <span class="pun">{</span><span class="pln">
			$srcUrl </span><span class="pun">=</span><span class="pln"> get_option</span><span class="pun">(</span><span class="str">'siteurl'</span><span class="pun">);</span>
			<span class="kwd">for</span> <span class="pun">(</span><span class="pln">$i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> $i </span><span class="pun"><</span><span class="pln"> count</span><span class="pun">(</span><span class="pln">$matches</span><span class="pun">);</span><span class="pln"> $i</span><span class="pun">++)</span>
			<span class="pun">{</span><span class="pln">
				$tag </span><span class="pun">=</span><span class="pln"> $matches</span><span class="pun">[</span><span class="pln">$i</span><span class="pun">][</span><span class="lit">0</span><span class="pun">];</span><span class="pln">
				$tag2 </span><span class="pun">=</span><span class="pln"> $matches</span><span class="pun">[</span><span class="pln">$i</span><span class="pun">][</span><span class="lit">0</span><span class="pun">];</span><span class="pln">
				$url </span><span class="pun">=</span><span class="pln"> $matches</span><span class="pun">[</span><span class="pln">$i</span><span class="pun">][</span><span class="lit">0</span><span class="pun">];</span><span class="pln">
				$noFollow </span><span class="pun">=</span> <span class="str">''</span><span class="pun">;</span><span class="pln">
				$pattern </span><span class="pun">=</span> <span class="str">'/target\s*=\s*"\s*_blank\s*"/'</span><span class="pun">;</span><span class="pln">
				preg_match</span><span class="pun">(</span><span class="pln">$pattern</span><span class="pun">,</span><span class="pln"> $tag2</span><span class="pun">,</span><span class="pln"> $match</span><span class="pun">,</span><span class="pln"> PREG_OFFSET_CAPTURE</span><span class="pun">);</span>
				<span class="kwd">if</span><span class="pun">(</span><span class="pln"> count</span><span class="pun">(</span><span class="pln">$match</span><span class="pun">)</span> <span class="pun"><</span> <span class="lit">1</span> <span class="pun">)</span><span class="pln">
					$noFollow </span><span class="pun">.=</span> <span class="str">' target="_blank" '</span><span class="pun">;</span><span class="pln">
				$pattern </span><span class="pun">=</span> <span class="str">'/rel\s*=\s*"\s*[n|d]ofollow\s*"/'</span><span class="pun">;</span><span class="pln">
				preg_match</span><span class="pun">(</span><span class="pln">$pattern</span><span class="pun">,</span><span class="pln"> $tag2</span><span class="pun">,</span><span class="pln"> $match</span><span class="pun">,</span><span class="pln"> PREG_OFFSET_CAPTURE</span><span class="pun">);</span>
				<span class="kwd">if</span><span class="pun">(</span><span class="pln"> count</span><span class="pun">(</span><span class="pln">$match</span><span class="pun">)</span> <span class="pun"><</span> <span class="lit">1</span> <span class="pun">)</span><span class="pln"> $noFollow </span><span class="pun">.=</span> <span class="str">' rel="<a href="http://www.laoking.net/tag/nofollow" title="查看更多关于nofollow的文章" target="_blank">nofollow</a>" '</span><span class="pun">;</span><span class="pln"> $pos </span><span class="pun">=</span><span class="pln"> strpos</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln">$srcUrl</span><span class="pun">);</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">$pos </span><span class="pun">===</span> <span class="kwd">false</span><span class="pun">)</span> <span class="pun">{</span><span class="pln"> $tag </span><span class="pun">=</span><span class="pln"> rtrim </span><span class="pun">(</span><span class="pln">$tag</span><span class="pun">,</span><span class="str">'>'</span><span class="pun">);</span><span class="pln">
					$tag </span><span class="pun">.=</span><span class="pln"> $noFollow</span><span class="pun">.</span><span class="str">'>'</span><span class="pun">;</span><span class="pln">
					$content </span><span class="pun">=</span><span class="pln"> str_replace</span><span class="pun">(</span><span class="pln">$tag2</span><span class="pun">,</span><span class="pln">$tag</span><span class="pun">,</span><span class="pln">$content</span><span class="pun">);</span>
				<span class="pun">}</span>
			<span class="pun">}</span>
		<span class="pun">}</span>
	<span class="pun">}</span><span class="pln">
	$content </span><span class="pun">=</span><span class="pln"> str_replace</span><span class="pun">(</span><span class="str">']]>'</span><span class="pun">,</span> <span class="str">']]>'</span><span class="pun">,</span><span class="pln"> $content</span><span class="pun">);</span>
	<span class="kwd">return</span><span class="pln"> $content</span><span class="pun">;</span>
<span class="pun">}</span>

好了,教程到此为止,有什么问题可以在下方留言告诉我!


老K博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:WordPress外链自动新窗口打开并添加nofollow属性
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址