本文章來給各位同學介紹關于如何處理wordpress博客中評論者發我鏈的行為,最終于我們確實自動把評論連接帶自動加上跳轉功能,在functions.php文件加入以下代碼:
- //評論鏈接重定向
- add_filter('get_comment_author_link', 'add_redirect_comment_link', 5);
- add_filter('comment_text', 'add_redirect_comment_link', 99);
- function add_redirect_comment_link($text = ''){
- $text=str_replace('href="', 'href="'.get_option('home').'/?r=', $text);
- $text=str_replace("href='", "href='".get_option('home')."/?r=", $text);
- return $text;
- }
- add_action('init', 'redirect_comment_link');
- function redirect_comment_link(){
- $redirect = $_GET['r'];
- if($redirect){
- if(strpos($_SERVER['HTTP_REFERER'],get_option('home')) !== false){
- header("Location: $redirect");
- exit;
- }
- else {
- header("Location: http://www.49028c.com");
- exit;
- }
- }
- }
在網站根目錄下的robots.txt文件中添加以下代碼,用來屏蔽搜索引擎收錄重定向后的網址.
Disallow: /?r=*再來看看前臺評論者的鏈接吧,都變成如下形式了
http://www.你的連接.com/?r=http://www.49028c.com
但如果你的博客使用的是多說插件,那以上所有寫的東西都是廢話,沒辦法對多說里的評論起作用.
直接給WordPress評論連接加上nofollow功能
找到wordpress的functions.php(wwwrootwp-contentthemesConcisePro1.3),然后在其中添加以下代碼,保存上傳即可,代碼如下:
- //評論加Nofollow
- function add_nofollow_to_comments_popup_link(){
- return ' rel="nofollow" ';
- }
- add_filter('comments_popup_link_attributes', 'add_nofollow_to_comments_popup_link');
順便優化以下,要加的地方有兩個,一個是評論者的鏈接,還有就是那個“回復”這也是個鏈接,代碼如下:
- //為評論者添加nofollow屬性
- function add_nofollow_to_comments_popup_link(){
- return 'rel="nofollow" ';
- }
- add_filter('comments_popup_link_attributes', 'add_nofollow_to_comments_popup_link');
- //為評論回復鏈接加nofollow屬性
- add_filter('comment_reply_link', 'add_nofollow_to_replay_link');
- function add_nofollow_to_replay_link( $link ){
- return str_replace( '")'>', '")' rel='nofollow'>', $link );
- }
新聞熱點
疑難解答
圖片精選