WordPress评论者链接默认是再原窗口打开的,这样的用户体验非常不好,很容易让访客跳出网站。今天来说说怎么让WordPress评论者链接在新窗口打开。
我们都知道给a标签加上target=’_blank’属性就能让链接在新窗口中打开(支持大部分浏览器)。那么问题就是找到Wordpress评论链接调用的地方。
我们发现WordPress通过get_comment_author_link函数用来获取评论人的链接。函数源码如下(/wp-includes/comment-template.php中):
/**
* Retrieve the HTML link to the URL of the author of the current comment.
*
* Both get_comment_author_url() and get_comment_author() rely on get_comment(),
* which falls back to the global comment variable if the $comment_ID argument is empty.
*
* @since 1.5.0
* @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
*
* @param int|WP_Comment $comment_ID Optional. WP_Comment or the ID of the comment for which to get the author's link.
* Default current comment.
* @return string The comment author name or HTML link for author's URL.
*/
function get_comment_author_link( $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 = "$author";
/**
* Filter the comment author's link for display.
*
* @since 1.5.0
* @since 4.1.0 The `$author` and `$comment_ID` parameters were added.
*
* @param string $return The HTML-formatted comment author link.
* Empty for an invalid URL.
* @param string $author The comment author's username.
* @param int $comment_ID The comment ID.
*/
return apply_filters( 'get_comment_author_link', $return, $author, $comment->comment_ID );
}
最简单的方法就是直接修改源码,将
$return = "$author";
修改成
$return = "$author";
在链接中加上target=’_blank’属性。保存文件试一试,是不是可以让评论人的链接在新窗口中打开了。但是这个方法唯一的缺点就是更新WordPress就要重新修改文件。所有这不是我们想要的。
如果你还记得apply_filters这个函数的用法。你就会想到利用钩子来修改WordPress评论者链接了吧!
在WordPress主题functions.php文件中加上以下代码:(已废弃)
//评论者的链接新窗口打开
function get_comment_author_link_new() {
global $comment;
$url = get_comment_author_url();
$author = get_comment_author();
if ( empty( $url ) || 'http://' == $url )
$return = $author;
else
$return = "$author";
return $return;
}
add_filter('get_comment_author_link', 'get_comment_author_link_new');
这样就能完美解决问题了。
2017/5/19更新
上面的代码有bug,下面是修订版本,请使用下面代码,具体原因请看重新认识一下add_filter的使用方法
//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 = "$author";
return $return;
}
add_filter('get_comment_author_link', 'get_comment_author_link_new', 10, 3);
感觉本站内容不错,读后有收获?小额赞助
还可以分享文章给好友:
谢谢大佬,测试可用!
推荐一个更好的办法:
“`php
function rt_add_link_target( $content ){
// use the tag to split into segments
$bits = explode( ‘$bit ){
// find the end of each link
$pos = strpos( $bit, ‘>’ );
// check if there is an end (only fails with malformed markup)
if( $pos !== false ){
// get a string with just the link’s attibutes
$part = substr( $bit, 0, $pos );
// for comparison, get the current site/network url
$siteurl = network_site_url();
// if the site url is in the attributes, assume it’s in the href and skip, also if a target is present
if( strpos( $part, $siteurl ) === false && strpos( $part, ‘target=’ ) === false ){
// add the target attribute
$bits[$key] = ‘target=”_blank” ‘ . $bits[$key];
}
}
}
// re-assemble the content, and return it
return implode( ‘<a ', $bits );
}
“`
感谢分享
添加这个功能对用户和网站都是有好处的
估计凡是使用wordpress的都要改这个。
嗯,这是刚需…