edit_comment_link()
云策文档标注
概述
edit_comment_link() 函数用于在 WordPress 中显示带有格式的评论编辑链接,仅当当前用户具有编辑评论权限时输出。该函数接受可选参数来自定义链接文本和前后内容。
关键要点
- 函数输出评论编辑链接,基于当前用户权限检查,使用 current_user_can('edit_comment', $comment->comment_ID)。
- 参数包括 $text(链接文本,默认 'Edit This')、$before(链接前内容)和 $after(链接后内容),均为可选。
- 内部使用 get_edit_comment_link() 生成链接,并通过 apply_filters('edit_comment_link', $link, $comment->comment_ID, $text) 应用过滤器。
- 相关函数包括 get_edit_comment_link()、current_user_can()、__()、esc_url()、apply_filters() 和 get_comment()。
- 在 Walker_Comment 类的方法中调用,用于评论输出。
代码示例
// 使用默认设置显示评论编辑链接
edit_comment_link();
// 自定义链接文本为 'Edit Comment',并包裹在段落标签中
edit_comment_link( __( 'Edit Comment', 'textdomain' ), '<p>', '</p>' );
原文内容
Displays the edit comment link with formatting.
Parameters
$textstringoptional-
Anchor text. If null, default is ‘Edit This’.
Default:
null $beforestringoptional-
Display before edit link. Default empty.
$afterstringoptional-
Display after edit link. Default empty.
Source
function edit_comment_link( $text = null, $before = '', $after = '' ) {
$comment = get_comment();
if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
return;
}
if ( null === $text ) {
$text = __( 'Edit This' );
}
$link = '<a class="comment-edit-link" href="' . esc_url( get_edit_comment_link( $comment ) ) . '">' . $text . '</a>';
/**
* Filters the comment edit link anchor tag.
*
* @since 2.3.0
*
* @param string $link Anchor tag for the edit link.
* @param string $comment_id Comment ID as a numeric string.
* @param string $text Anchor text.
*/
echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID, $text ) . $after;
}
Hooks
- apply_filters( ‘edit_comment_link’, string $link, string $comment_id, string $text )
-
Filters the comment edit link anchor tag.
Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |
Skip to note 2 content
Codex
Examples
Displays edit comment link using defaults.
Displays edit comment link, with link text “edit comment”, in a paragraph () tag.
edit_comment_link( __( 'Edit Comment', 'textdomain' ), '<p>', '</p>' );