WordPress教程

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

阿里云

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

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

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

也想出现在这里?联系我们
创客主机
  1. function get_comment_author_link( $comment_ID = 0 ) {
  2.     $comment = get_comment( $comment_ID );
  3.     $url     = get_comment_author_url( $comment );
  4.     $author  = get_comment_author( $comment );
  5.  
  6.     if ( empty( $url ) || 'http://' == $url ) {
  7.         $return = $author;
  8.     } else {
  9.         $return = "<a href='$url' rel='external nofollow ugc' class='url'>$author</a>";
  10.     }
  11.  
  12.     /**
  13.      * Filters the comment author's link for display.
  14.      *
  15.      * @since 1.5.0
  16.      * @since 4.1.0 The `$author` and `$comment_ID` parameters were added.
  17.      *
  18.      * @param string $return     The HTML-formatted comment author link.
  19.      *                           Empty for an invalid URL.
  20.      * @param string $author     The comment author's username.
  21.      * @param int    $comment_ID The comment ID.
  22.      */
  23.     return apply_filters( 'get_comment_author_link', $return, $author, $comment->comment_ID );
  24. }

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

  1. function get_comment_author( $comment_ID = 0 ) {
  2.     $comment = get_comment( $comment_ID );
  3.  
  4.     if ( empty( $comment->comment_author ) ) {
  5.         $user = $comment->user_id ? get_userdata( $comment->user_id ) : false;
  6.         if ( $user ) {
  7.             $author = $user->display_name;
  8.         } else {
  9.             $author = __( 'Anonymous' );
  10.         }
  11.     } else {
  12.         $author = $comment->comment_author;
  13.     }
  14.  
  15.     /**
  16.      * Filters the returned comment author name.
  17.      *
  18.      * @since 1.5.0
  19.      * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
  20.      *
  21.      * @param string     $author     The comment author's username.
  22.      * @param int        $comment_ID The comment ID.
  23.      * @param WP_Comment $comment    The comment object.
  24.      */
  25.     return apply_filters( 'get_comment_author', $author, $comment->comment_ID, $comment );
  26. }

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

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

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

  1. /**
  2.  * 将评论作者名称显示为昵称
  3.  */
  4. function wpkj_get_comment_author_filter( $author, $comment_ID, $comment ){
  5.  
  6.     $user = $comment->user_id ? get_userdata( $comment->user_id ) : false;
  7.     if ( $user ) {
  8.         $author = $user->nickname;
  9.     } else {
  10.         $author = __( 'Anonymous' );
  11.     }
  12.  
  13.     return $author;
  14. }
  15. add_filter( 'get_comment_author', 'wpkj_get_comment_author_filter', 10, 3 );

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

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

已有 342 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!