在 WordPress 中,您可以在页面加载之前执行以下操作来编辑文章的内容:
add_filter('the_content', 'edit_content');
function edit_content($content) {
// edit $content
return $content;
}
除了评论,我怎样才能达到同样的效果?
最佳答案
您将过滤器添加到 comment_text
Hook ,类似于您使用 the_content
Hook 的方式。此 Hook 允许您更改在模板中使用 comment_text()
显示的评论文本。
例如:
add_filter( 'comment_text', 'edit_comment_text', 99);
function edit_comment_text( $comment_text, $commentObject, $args ) {
// edit the text....
return $comment_text;
}
请注意,您可能需要将 add_filter
中的优先级设置为较高的数字,以便它首先在 WP 自己的过滤器之后运行 - 我在上面使用了 99
。
你可以在WP Developer Code Reference for comment_text
中看到过滤器获取的 3 个参数是:
$comment_text
(字符串)- 当前评论的文本。$commentObject
(WP_Comment|null) - 评论对象。如果找不到则为空。$args
(数组)- 参数数组。
关于php - 如何在页面呈现之前编辑 WordPress 评论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63839158/