建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

WordPress回复评论自动添加@commenter

GG网络技术分享 2025-03-18 16:05 0


回复评论者的消息时,请在消息前添加@commenter,以便更好地说明您要回复的对象。

也许网上有相关的教程,不过我这里也分享一下我的实现方法。 将以下代码添加到当前主题的functions.php中:

function mobantu_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' , 'mobantu_comment_add_at', 20);

以上代码会直接将@信息写入数据库,如果不想将@commenter写入数据库,可以使用如下代码:

function mobantu_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' , 'mobantu_comment_add_at', 20, 2);

标签:

提交需求或反馈

Demand feedback