[WordPress]WordPress 给评论者链接添加nofollow 新窗口打开以及GO跳转-第1张

前段时间给网站外链添加了nofollow,以及设置了新窗口打开,WordPress文章外链自动新窗口打开并添加nofollow属性

发现评论者链接是直接在本窗口打开的,体验极其不好,而且链接没有添加nofollow,于是便想着改造一下。百度搜集资料,找到了解决办法。具体原理不在解释,以下是解决办法。

直接在主题function.php文件中添加以下代码,便可以解决nofollow以及新窗口打开的问题。

//WordPress评论者的链接新窗口打开
function get_comment_author_link_new($return, $author, $comment_ID = 0) {
	$comment = get_comment( $comment_ID );
	$url     = get_comment_author_url( $comment );
	$author  = get_comment_author( $comment );
 
	if ( empty( $url ) || 'http://' == $url )
		$return = $author;
	else
		$return = "<a href='$url' target='_blank' rel='external nofollow' class='url'>$author</a>";
 
	return $return;
}
add_filter('get_comment_author_link', 'get_comment_author_link_new', 10, 3);

感觉还不爽,文章内容已经开启go跳转了,那么我是不是可以直接调用这个go跳转,为评论者链接添加go跳转呢?答案是肯定的,对上述代码进行了如下的修改。

//WordPress评论者的链接新窗口打开+GO跳转
function get_comment_author_link_new($return, $author, $comment_ID = 0) {
$comment = get_comment( $comment_ID );
$url = get_comment_author_url( $comment );
$author = get_comment_author( $comment );

if ( empty( $url ) || 'http://' == $url )
$return = $author;
else

$return = "<a href='$url' target='_blank' rel='external nofollow' class='url'>$author</a>";
$return=str_replace("href='$url'", "href=\"".home_url()."/go/?url=$url\" ",$return);//添加go跳转

return $return;
}
add_filter('get_comment_author_link', 'get_comment_author_link_new', 10, 3);

上述代码调用了本站先前设置的go跳转,其中代码含义是调用了根目录go文件夹下的index.php文件,这个index.php文件会自动跳转到你设置的链接中。查看外链go跳转页面教程合集。

声明:本站文章原创有部分资源来源于网络,如无特殊说明或标注。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系 admin@liitk.com 进行删除处理!。