WordPress 回复评论自动添加@评论者

WordPress 回复评论自动添加@评论者,这有点类似微博@的效果,实现方法很简单:

「后台」→「外观」→「编辑」→ 「functions.php」文件,把下面的代码添加进去:

// 评论添加@
function ludou_comment_add_at( $commentdata ) {
  if( $commentdata['comment_parent'] > 0) {
    $commentdata['comment_content'] = '@<a href="#comment-' . $commentdata['comment_parent'] . '">'.get_comment_author( $commentdata['comment_parent'] ) . '</a> ' . $commentdata['comment_content'];
  }

  return $commentdata;
}
add_action( 'preprocess_comment' , 'ludou_comment_add_at', 20);

以上代码会直接将 @ 信息写入数据库。如果你不想将 @评论者 写入数据库,可以使用下面的代码:

// 评论添加@
function ludou_comment_add_at( $comment_text, $comment = '') {
  if( $comment->comment_parent > 0) {
    $comment_text = '@<a href="#comment-' . $comment->comment_parent . '">'.get_comment_author( $comment->comment_parent ) . '</a> ' . $comment_text;
  }

  return $comment_text;
}
add_filter( 'comment_text' , 'ludou_comment_add_at', 20, 2);

# 更多WordPress技巧,请关注「WordPress专题