wp_comment_reply()
概述
wp_comment_reply() 函数用于在评论列表表格中输出内联评论回复表单。它支持自定义参数来控制表单的显示和行为,并包含一个过滤器以允许开发者修改输出内容。
关键要点
- 函数 wp_comment_reply() 输出评论回复表单,适用于 WordPress 后台的评论管理界面。
- 接受四个可选参数:$position(位置,默认 1)、$checkbox(复选框,默认 false)、$mode(模式,默认 'single')和 $table_row(表格行,默认 true)。
- 包含 apply_filters('wp_comment_reply', ...) 钩子,允许开发者通过返回非空值来短路默认表单输出。
- 根据 $mode 参数动态选择 WP_Post_Comments_List_Table 或 WP_Comments_List_Table 类实例。
- 输出包含表单元素、编辑器、非ce字段和权限检查,确保安全性和功能性。
代码示例
function wp_comment_reply( $position = 1, $checkbox = false, $mode = 'single', $table_row = true ) {
global $wp_list_table;
$content = apply_filters(
'wp_comment_reply',
'',
array(
'position' => $position,
'checkbox' => $checkbox,
'mode' => $mode,
)
);
if ( ! empty( $content ) ) {
echo $content;
return;
}
// 更多代码...
}注意事项
- 使用前需确保全局变量 $wp_list_table 已设置或可通过 _get_list_table() 获取。
- 过滤器 wp_comment_reply 允许完全自定义表单输出,但需谨慎处理以避免破坏默认功能。
- 参数 $table_row 控制是否使用表格元素,可能影响前端样式和布局。
Outputs the in-line comment reply-to form in the Comments list table.
Parameters
$positionintoptional-
The value of the
'position'input field.Default:
1 $checkboxbooloptional-
The value of the
'checkbox'input field.Default:
false $modestringoptional-
If set to
'single', will use WP_Post_Comments_List_Table, otherwise WP_Comments_List_Table. Default'single'. $table_rowbooloptional-
Whether to use a table instead of a div element.
Default:
true
Source
function wp_comment_reply( $position = 1, $checkbox = false, $mode = 'single', $table_row = true ) {
global $wp_list_table;
/**
* Filters the in-line comment reply-to form output in the Comments
* list table.
*
* Returning a non-empty value here will short-circuit display
* of the in-line comment-reply form in the Comments list table,
* echoing the returned value instead.
*
* @since 2.7.0
*
* @see wp_comment_reply()
*
* @param string $content The reply-to form content.
* @param array $args An array of default args.
*/
$content = apply_filters(
'wp_comment_reply',
'',
array(
'position' => $position,
'checkbox' => $checkbox,
'mode' => $mode,
)
);
if ( ! empty( $content ) ) {
echo $content;
return;
}
if ( ! $wp_list_table ) {
if ( 'single' === $mode ) {
$wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table' );
} else {
$wp_list_table = _get_list_table( 'WP_Comments_List_Table' );
}
}
?>
<form method="get">
<table style="display:none;"><tbody id="com-reply"><tr id="replyrow" class="inline-edit-row" style="display:none;"><td colspan="<?php echo $wp_list_table->get_column_count(); ?>" class="colspanchange">
<div id="com-reply" style="display:none;"><div id="replyrow" style="display:none;">
<fieldset class="comment-reply">
<legend>
<span class="hidden" id="editlegend"></span>
<span class="hidden" id="replyhead"></span>
<span class="hidden" id="addhead"></span>
</legend>
<div id="replycontainer">
<label for="replycontent" class="screen-reader-text">
</label>
'strong,em,link,block,del,ins,img,ul,ol,li,code,close' );
wp_editor(
'',
'replycontent',
array(
'media_buttons' => false,
'tinymce' => false,
'quicktags' => $quicktags_settings,
)
);
?>
</div>
<div id="edithead" style="display:none;">
<div class="inside">
<label for="author-name"></label>
<input type="text" name="newcomment_author" size="50" value="" id="author-name" />
</div>
<div class="inside">
<label for="author-email"></label>
<input type="text" name="newcomment_author_email" size="50" value="" id="author-email" />
</div>
<div class="inside">
<label for="author-url"></label>
<input type="text" id="author-url" name="newcomment_author_url" class="code" size="103" value="" />
</div>
</div>
<div id="replysubmit" class="submit">
<p class="reply-submit-buttons">
<button type="button" class="save button button-primary">
<span id="addbtn" style="display: none;"></span>
<span id="savebtn" style="display: none;"></span>
<span id="replybtn" style="display: none;"></span>
</button>
<button type="button" class="cancel button"></button>
<span class="waiting spinner"></span>
</p>
</p>',
array(
'type' => 'error',
'additional_classes' => array( 'notice-alt', 'inline', 'hidden' ),
'paragraph_wrap' => false,
)
);
?>
</div>
<input type="hidden" name="action" id="action" value="" />
<input type="hidden" name="comment_ID" id="comment_ID" value="" />
<input type="hidden" name="comment_post_ID" id="comment_post_ID" value="" />
<input type="hidden" name="status" id="status" value="" />
<input type="hidden" name="position" id="position" value="<?php echo $position; ?>" />
<input type="hidden" name="checkbox" id="checkbox" value="<?php echo $checkbox ? 1 : 0; ?>" />
<input type="hidden" name="mode" id="mode" value="<?php echo esc_attr( $mode ); ?>" />
</fieldset>
</td></tr></tbody></table>
</div></div>
</form>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/template.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/template.php#L413">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/template.php#L413-L548">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-hooks"><h2 id="hooks" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#hooks">Hooks</a></h2> <dl><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/wp_comment_reply/"><span class="hook-func">apply_filters</span>( ‘wp_comment_reply’, <nobr><span class="arg-type">string</span> <span class="arg-name">$content</span></nobr>, <nobr><span class="arg-type">array</span> <span class="arg-name">$args</span></nobr> )</a></dt><dd><p>Filters the in-line comment reply-to form output in the Comments list table.</p>
</dd></dl></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/wp_admin_notice/">wp_admin_notice()</a><code>wp-includes/functions.php
Outputs an admin notice.
WP_List_Table::get_column_count()wp-admin/includes/class-wp-list-table.php
Returns the number of visible columns.
_get_list_table()wp-admin/includes/list-table.php
Fetches an instance of a WP_List_Table class.
wp_editor()wp-includes/general-template.php
Renders an editor.
wp_nonce_field()wp-includes/functions.php
Retrieves or display nonce hidden field for forms.
current_user_can()wp-includes/capabilities.php
Returns whether the current user has the specified capability.
_e()wp-includes/l10n.php
Displays translated text.
esc_attr()wp-includes/formatting.php
Escaping for HTML attributes.
apply_filters()wp-includes/plugin.php
Calls the callback functions that have been added to a filter hook.
| Used by | Description |
|---|---|
wp_dashboard_recent_comments()wp-admin/includes/dashboard.php |
Show Comments section. |
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |