外贸独立站的底层设计密码WordPress 成品站模板
当前位置:首页>WordPress建站>WordPress开发>WordPress强制调用用户昵称作为评论作者的名字

WordPress强制调用用户昵称作为评论作者的名字

默认情况下,在WordPress评论中,对于登录用户发表的评论,会使用“公开显示为”(display_name)字段的值作为用户的显示名称。如果用户不能修改/或者没有修改这个“公开显示为”选项,就会显示用户的登录名(user_login)。

如果我们希望强制采用“昵称 nickname”作为评论作者的用户名称,该怎么办呢?下面我们将分析思路详细说一下,想直接用代码的就看最后的代码片段即可。

通过检查发现,评论作者的名字和链接部分是通过 get_comment_author_link()函数输出的,代码如下:

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 = "<a href='$url' rel='external nofollow ugc' class='url'>$author</a>";
    }
 
    /**
     * Filters 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 );
}

在代码的第4行,我们可以看到 $author = get_comment_author( $comment );来获取评论作者名称,接着看 get_comment_author() 函数的代码:

function get_comment_author( $comment_ID = 0 ) {
    $comment = get_comment( $comment_ID );
 
    if ( empty( $comment->comment_author ) ) {
        $user = $comment->user_id ? get_userdata( $comment->user_id ) : false;
        if ( $user ) {
            $author = $user->display_name;
        } else {
            $author = __( 'Anonymous' );
        }
    } else {
        $author = $comment->comment_author;
    }
 
    /**
     * Filters the returned comment author name.
     *
     * @since 1.5.0
     * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
     *
     * @param string     $author     The comment author's username.
     * @param int        $comment_ID The comment ID.
     * @param WP_Comment $comment    The comment object.
     */
    return apply_filters( 'get_comment_author', $author, $comment->comment_ID, $comment );
}

在代码的第7行,可以看到调用的是 display_name (即“公开显示为”)然后底部有一个钩子:

apply_filters( 'get_comment_author', $author, $comment->comment_ID, $comment );

我们下来要做的,就是通过钩子去修改为昵称。将下面的代码添加到主题的 functions.php 文件或你的插件文件中,就可以达到目的:

/**
 * 将评论作者名称显示为昵称
 */
function wpkj_get_comment_author_filter( $author, $comment_ID, $comment ){

    $user = $comment->user_id ? get_userdata( $comment->user_id ) : false;
    if ( $user ) {
        $author = $user->nickname;
    } else {
        $author = __( 'Anonymous' );
    }

    return $author;
}
add_filter( 'get_comment_author', 'wpkj_get_comment_author_filter', 10, 3 );

当然了,如果用户没有设置过昵称,那还是会显示用户的登录名哦。

拓展阅读:

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
WordPress开发

WordPress和WooCommerce内置的Cookies大全

2020-4-6 10:20:16

WordPress开发

WordPress自定义古腾堡编辑器的渐变色

2020-4-19 9:31:54

2 条回复 A文章作者 M管理员
  1. 阿里舞台

    好了,虽然是代码小白,也经不住我瞎折腾啊!使用这个代码引起的普通访客显示成匿名问题已经解决了,不过这似乎发不了代码吧,我放链接也不知道能不能成功。先试试再说。https://www.aliwutai.com/23829.html
    已经可以实现注册用户显示昵称,访客显示他所填写的昵称的功能,不会把访客填写的昵称统一转换为匿名了。

  2. 阿里舞台

    这代码会遇到一个问题,就是访客评论的,会显示成匿名了。别的都很好。当然如果对管理员显示成用户名(昵称)这样会更好。有时候用户留言后,不得不去后台搜索一下看这人是谁。

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索