get_comment_reply_link()
云策文档标注
概述
get_comment_reply_link() 函数用于生成评论回复链接的 HTML 内容。它接受参数数组、评论对象和文章对象,返回链接字符串或 false/null 表示失败。
关键要点
- 函数返回评论回复链接的 HTML,成功时返回字符串,评论关闭时返回 false,失败时返回 null。
- 参数 $args 可覆盖默认设置,包括 add_below、respond_id、reply_text、reply_to_text、login_text、max_depth、depth、before、after 和 show_reply_to_text。
- 参数 $comment 指定被回复的评论,默认为当前评论;$post 指定评论所属文章,默认为当前文章。
- 函数内部处理评论深度、评论开放状态、用户登录状态等逻辑,并生成包含 data 属性和 aria-label 的链接。
- 提供两个过滤器:comment_reply_link_args 用于过滤参数,comment_reply_link 用于过滤最终链接。
代码示例
// 基本用法
echo get_comment_reply_link();
// 自定义参数
$args = array(
'reply_text' => '回复',
'depth' => 2,
'max_depth' => 5
);
echo get_comment_reply_link($args, $comment, $post);注意事项
- 确保在评论循环中调用此函数,以便正确获取 $comment 和 $post 参数。
- 使用 show_reply_to_text 参数时,reply_to_text 应包含 %s 占位符以插入评论作者名。
- 函数依赖于 WordPress 的评论系统和相关函数,如 comments_open() 和 get_comment_author()。
原文内容
Retrieves HTML content for reply to comment link.
Parameters
$argsarrayoptional-
Override default arguments.
add_belowstringThe first part of the selector used to identify the comment to respond below.
The resulting value is passed as the first parameter to addComment.moveForm(), concatenated as $add_below-$comment->comment_ID. Default'comment'.respond_idstringThe selector identifying the responding comment. Passed as the third parameter to addComment.moveForm(), and appended to the link URL as a hash value.
Default'respond'.reply_textstringThe visible text of the Reply link. Default'Reply'.reply_to_textstringThe accessible name of the Reply link, using%sas a placeholder for the comment author’s name. Default ‘Reply to %s’.
Should start with the visiblereply_textvalue.show_reply_to_textboolWhether to usereply_to_textas visible link text. Default false.login_textstringThe text of the link to reply if logged out. Default ‘Log in to Reply’.max_depthintThe max depth of the comment tree. Default 0.depthintThe depth of the new comment. Must be greater than 0 and less than the value of the'thread_comments_depth'option set in Settings > Discussion. Default 0.beforestringThe text or HTML to add before the reply link.afterstringThe text or HTML to add after the reply link.
Default:
array() $commentint|WP_Commentoptional-
Comment being replied to. Default current comment.
Default:
null $postint|WP_Postoptional-
Post ID or WP_Post object the comment is going to be displayed on.
Default current post.Default:
null
Source
function get_comment_reply_link( $args = array(), $comment = null, $post = null ) {
$defaults = array(
'add_below' => 'comment',
'respond_id' => 'respond',
'reply_text' => __( 'Reply' ),
/* translators: Comment reply button text. %s: Comment author name. */
'reply_to_text' => __( 'Reply to %s' ),
'login_text' => __( 'Log in to Reply' ),
'max_depth' => 0,
'depth' => 0,
'before' => '',
'after' => '',
'show_reply_to_text' => false,
);
$args = wp_parse_args( $args, $defaults );
$args['max_depth'] = (int) $args['max_depth'];
$args['depth'] = (int) $args['depth'];
if ( 0 === $args['depth'] || $args['max_depth'] <= $args['depth'] ) {
return null;
}
$comment = get_comment( $comment );
if ( empty( $comment ) ) {
return null;
}
if ( empty( $post ) ) {
$post = $comment->comment_post_ID;
}
$post = get_post( $post );
if ( ! comments_open( $post->ID ) ) {
return false;
}
if ( get_option( 'page_comments' ) ) {
$permalink = str_replace( '#comment-' . $comment->comment_ID, '', get_comment_link( $comment ) );
} else {
$permalink = get_permalink( $post->ID );
}
/**
* Filters the comment reply link arguments.
*
* @since 4.1.0
*
* @param array $args Comment reply link arguments. See get_comment_reply_link()
* for more information on accepted arguments.
* @param WP_Comment $comment The object of the comment being replied to.
* @param WP_Post $post The WP_Post object.
*/
$args = apply_filters( 'comment_reply_link_args', $args, $comment, $post );
if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
$link = sprintf(
'<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
esc_url( wp_login_url( get_permalink() ) ),
$args['login_text']
);
} else {
$data_attributes = array(
'commentid' => $comment->comment_ID,
'postid' => $post->ID,
'belowelement' => $args['add_below'] . '-' . $comment->comment_ID,
'respondelement' => $args['respond_id'],
'replyto' => sprintf( $args['reply_to_text'], get_comment_author( $comment ) ),
);
$data_attribute_string = '';
foreach ( $data_attributes as $name => $value ) {
$data_attribute_string .= " data-{$name}="" . esc_attr( $value ) . '"';
}
$data_attribute_string = trim( $data_attribute_string );
$reply_text = $args['show_reply_to_text']
? sprintf( $args['reply_to_text'], get_comment_author( $comment ) )
: $args['reply_text'];
$aria_label = $args['show_reply_to_text'] ? '' : sprintf( $args['reply_to_text'], get_comment_author( $comment ) );
$link = sprintf(
'<a rel="nofollow" class="comment-reply-link" href="%s" %s%s>%s</a>',
esc_url(
add_query_arg(
array(
'replytocom' => $comment->comment_ID,
'unapproved' => false,
'moderation-hash' => false,
),
$permalink
)
) . '#' . $args['respond_id'],
$data_attribute_string,
$aria_label ? ' aria-label="' . esc_attr( $aria_label ) . '"' : '',
$reply_text
);
}
$comment_reply_link = $args['before'] . $link . $args['after'];
/**
* Filters the comment reply link.
*
* @since 2.7.0
*
* @param string $comment_reply_link The HTML markup for the comment reply link.
* @param array $args An array of arguments overriding the defaults.
* @param WP_Comment $comment The object of the comment being replied.
* @param WP_Post $post The WP_Post object.
*/
return apply_filters( 'comment_reply_link', $comment_reply_link, $args, $comment, $post );
}
Hooks
- apply_filters( ‘comment_reply_link’, string $comment_reply_link, array $args, WP_Comment $comment, WP_Post $post )
-
Filters the comment reply link.
- apply_filters( ‘comment_reply_link_args’, array $args, WP_Comment $comment, WP_Post $post )
-
Filters the comment reply link arguments.
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Added the ability for $comment to also accept a WP_Comment object. |
| 2.7.0 | Introduced. |