WordPress自動給文章添加nofollow屬性的實現(xiàn)方法
發(fā)布時間:2014-12-25 11:11:49 作者:佚名
我要評論

這篇文章主要為大家介紹了WordPress自動給文章添加nofollow屬性的實現(xiàn)方法,可通過Nofollow for external link 插件實現(xiàn)文章頁自動添加nofollow屬性的功能,是非常實用的技巧,需要的朋友可以參考下
本文實例講述了WordPress自動給文章添加nofollow屬性的實現(xiàn)方法。分享給大家供大家參考。具體分析如下:
nofollow屬性是告訴搜索引擎不傳權重過去,但WordPressk中如果我們要nofollow屬性就需要手工加了,現(xiàn)在我來告訴大家利用 Nofollow for external link就可以自動給文章添加nofollow屬性了.
直接安裝啟用 Nofollow for external link 插件,或者將下面的代碼添加到當前主題的 functions.php 文件即可.
實例代碼如下:
復制代碼
代碼如下:add_filter( 'the_content', 'cn_nf_url_parse');
function cn_nf_url_parse( $content ) {
$regexp = "<as[^>]*href=("??)([^" >]*?)\1[^>]*>";
if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
if( !emptyempty($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 = '/targets*=s*"s*_blanks*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$noFollow .= ' target="_blank" ';
$pattern = '/rels*=s*"s*[n|d]ofollows*"/';
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;
}
function cn_nf_url_parse( $content ) {
$regexp = "<as[^>]*href=("??)([^" >]*?)\1[^>]*>";
if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
if( !emptyempty($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 = '/targets*=s*"s*_blanks*"/';
preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$noFollow .= ' target="_blank" ';
$pattern = '/rels*=s*"s*[n|d]ofollows*"/';
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;
}
最終效果:自動給文章/頁面的站外鏈接添加nofollow屬性(rel=”nofollow”),并且在新窗口打開這些鏈接(即添加 target=”_blank”屬性),如果已經(jīng)手動給鏈接添加了 rel=”dofollow”,就不會添加 rel=”nofollow”,如果手動添加了 target=”_blank”,就不會重復添加.
為指定分類的所有鏈接添加nofollow屬性,那你可以將下面的代碼添加到主題的 functions.php 文件即可:
復制代碼
代碼如下:function nofollow_cat_posts($text) {
global $post;
if( in_category(1) ) { // 修改這里的分類ID
$text = stripslashes(wp_rel_nofollow($text));
}
return $text;
}
add_filter('the_content', 'nofollow_cat_posts');
global $post;
if( in_category(1) ) { // 修改這里的分類ID
$text = stripslashes(wp_rel_nofollow($text));
}
return $text;
}
add_filter('the_content', 'nofollow_cat_posts');
希望本文所述對大家的WordPress建站有所幫助。
相關文章
- 在瀏覽網(wǎng)頁的時候, 我們經(jīng)常會看到很多人在他們網(wǎng)站的搜索欄里顯示文字提示, 當鼠標點擊搜索欄, 則提示信息消失.2011-05-10
- 本文詳介如何給WordPress投稿功能添加郵件提醒功能2012-09-28
為WordPress添加文章字數(shù)統(tǒng)計的方法
下面看一下如何統(tǒng)計文章字數(shù)2012-10-06- 這篇文章主要為大家介紹了WordPress文章標題鏈接添加正在加載中提示的方法,可通過添加簡單的js腳本實現(xiàn)該功能,是比較實用的技巧,具有一定的參考借鑒價值,需要的朋友可以參2014-12-20
- 這篇文章主要為大家介紹了wordpress主題評論中添加回復的方法,可以無需通過插件來實現(xiàn)增加評論回復功能,是非常實用的技巧,需要的朋友可以參考下2014-12-20
- 這篇文章主要為大家介紹了WordPress文章底部去除與添加版權信息的方法,涉及個性化定制頁面的技巧,非常具有實用價值,需要的朋友可以參考下2014-12-19
- 這篇文章主要為大家介紹了WordPress中添加語音搜索功能的實現(xiàn)方法,屬于html5 的speech功能的API,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-18
- 這篇文章主要為大家介紹了WordPress添加前臺注冊功能的方法,較為詳細的分析了前臺注冊功能的完整實現(xiàn)過程,是進行WordPress開發(fā)非常實用的技巧,需要的朋友可以參考下2014-12-17
wordpress編輯器中添加鏈接功能自動加入nofollow的方法
這篇文章主要介紹了wordpress編輯器中添加鏈接功能自動加入nofollow的方法,需要的朋友可以參考下2014-04-10- 如何在wordpress的文章末尾添加內(nèi)容,在wordpress每篇文章的末尾添加文字、鏈接等內(nèi)容,比如你想加個“原創(chuàng)文章如轉載,請注明本文鏈接:”,其實很簡單,有多種方法,下面2013-11-13