comment_form
云策文档标注
概述
comment_form 是一个 WordPress 模板 Hook,在 comment_form() 函数渲染的评论表单底部、关闭 </form> 标签之前触发。它允许开发者在评论表单中添加自定义内容或功能。
关键要点
- comment_form 是一个 action Hook,在评论表单底部触发,位于关闭 </form> 标签内部。
- 钩子函数接收一个参数:$post_id(文章 ID),用于标识评论表单所属的文章。
- 通过 add_action() 将自定义函数挂载到此 Hook,以在评论表单中添加额外元素或处理逻辑。
代码示例
/**
* Displays an extra text area for more comments.
*
* @param int $post_id The ID of the post where the comment form was rendered.
*/
function wporg_more_comments( $post_id ) {
echo '<textarea name="more_comments">' . __( 'More Comments', 'your-theme-text-domain' ) . '</textarea>';
}
add_action( 'comment_form', 'wporg_more_comments' );注意事项
- 此 Hook 从 WordPress 1.5.0 版本引入,兼容性良好。
- 相关函数 comment_form() 位于 wp-includes/comment-template.php 中,用于输出完整的评论表单模板。
- 开发时需确保自定义函数正确处理 $post_id 参数,以避免潜在错误。
原文内容
Fires at the bottom of the comment form, inside the closing form tag.
Parameters
$post_idint-
The post ID.
Source
do_action( 'comment_form', $post_id );
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
Skip to note 2 content
Collins Mbaka
/** * Displays an extra text area for more comments. * * @param int $post_id The ID of the post where the comment form was rendered. */ function wporg_more_comments( $post_id ) { echo '<p class="comment-form-more-comments"><label for="more-comments">' . __( 'More Comments', 'your-theme-text-domain' ) . '</label> <textarea id="more-comments" name="more-comments" cols="45" rows="8" aria-required="true"></textarea></p>'; } add_action( 'comment_form', 'wporg_more_comments' );