修改WordPress自带评论框评论者所留链接,较少权重流失 😯
前言
评论者在留下了链接的时候除了会给自己带来流量之外,还可能会给我们流失权重,而且链接看起来不怎么漂亮,使用这段代码可以重写评论链接,比如https://gitcafe.net/?r=http://www.baidu.com 类似的
重写评论链接代码
//评论链接重写 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; } 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: ".bloginfo('url')."/"); exit; } } } add_action('init', 'redirect_comment_link'); add_filter('get_comment_author_link', 'add_redirect_comment_link', 5); add_filter('comment_text', 'add_redirect_comment_link', 99);
给评论链接添加nofollow属性
其实上面的代码好像也可以添加nofollow的,如果你没有使用的话,也可以用这个给评论链接添加nofollow属性
//给评论链接添加nofollow属性 function add_nofollow_to_comments_popup_link(){ return ' rel="nofollow" '; } add_filter('comments_popup_link_attributes', 'add_nofollow_to_comments_popup_link'); function add_nofollow_to_replay_link( $link ){ return str_replace( '")'>', '")' rel='nofollow'>', $link ); } add_filter('comment_reply_link', 'add_nofollow_to_replay_link');
-- 完 --
这些代码粘贴到哪里!那个文件夹啊!
@纬创博客 主题目录functions.php
直接放到主函数文件里面?
@atests 嗯
wordpress 折腾达人
@harries 毫无嘿嘿
@harries 加个头像差点没认出来