钩子文档

pre_comment_approved

💡 云策文档标注

概述

pre_comment_approved 是一个 WordPress 过滤器钩子,用于在评论被插入数据库前,过滤其批准状态。它允许插件覆盖 wp_allow_comment() 函数设置的初始批准状态,控制评论是否被批准、待审、标记为垃圾或移至回收站。

关键要点

  • pre_comment_approved 钩子在 wp_allow_comment() 函数中调用,用于过滤评论的批准状态。
  • 参数包括 $approved(批准状态,可接受 1、0、'spam'、'trash' 或 WP_Error)和 $commentdata(评论数据数组)。
  • 返回值为 0(待审)、1(批准)、'spam'(垃圾)或 'trash'(回收站),所有评论都会被添加到数据库,包括垃圾评论。
  • 从 WordPress 4.9.0 开始,返回 WP_Error 可以短路评论插入过程,跳过进一步处理。
  • $commentdata 数组包含评论的详细信息,如 comment_post_ID、comment_content 等,与 WP_Comment_Query::get_comments() 返回的索引一致。
  • 在 WordPress 3.1 之前,此过滤器不传递 $commentdata,而是使用全局变量如 $comment_ID 访问评论信息。

代码示例

// 示例:根据评论内容修改批准状态
add_filter('pre_comment_approved', 'my_comment_approval_filter', 10, 2);
function my_comment_approval_filter($approved, $commentdata) {
    // 检查评论内容是否包含特定关键词
    if (strpos($commentdata['comment_content'], 'spam_keyword') !== false) {
        return 'spam'; // 标记为垃圾
    }
    return $approved; // 否则保持原批准状态
}

注意事项

  • 此过滤器允许插件干预评论批准流程,但需谨慎处理以避免影响正常评论功能。
  • 返回 WP_Error 可以阻止评论插入,适用于需要严格验证的场景。
  • 垃圾评论虽被标记,但仍存储在数据库中,可供插件分析使用。

📄 原文内容

Filters a comment’s approval status before it is set.

Parameters

$approvedint|string|WP_Error
The approval status. Accepts 1, 0, 'spam', 'trash', or WP_Error.
$commentdataarray
Comment data.

More Information

  • A filter hook called by the wp_allow_comment() function prior to inserting a comment into the database. The filter is applied to the proposed comment’s approval status, allowing a plugin to override.
  • wp_allow_comment() handles the preliminary approval checking, and that approval status is passed through this filter before it returns.
  • The $commentdata array contains the same indices as the array returned by WP_Comment_Query::get_comments(), including:
    <br>
    'comment_post_ID' - The post to which the comment will apply<br>
    'comment_author' - (may be empty)<br>
    'comment_author_email' - (may be empty)<br>
    'comment_author_url' - (may be empty)<br>
    'comment_author_IP' - IP address<br>
    'comment_agent' - e.g., "Mozilla/5.0..."<br>
    'comment_content' - The text of the proposed comment<br>
    'comment_type' - 'pingback', 'trackback', or empty for regular comments<br>
    'user_ID' - (empty if not logged in)<br>
  • Return Values:
    <br>
    0 (int) comment is marked for moderation as "Pending"<br>
    1 (int) comment is marked for immediate publication as "Approved"<br>
    'spam' (string) comment is marked as "Spam"<br>
    'trash' (string) comment is to be put in the Trash<br>

    In all cases the comment is added to the database, even spam. Comments marked as spam will never be visible on the front end. Spam comments are kept for possible analysis by plugins.
  • Prior to WP 3.1, the filter was not passed $comment_data and instead was expected to use global variables such as $comment_ID to access information about the comment. (see: https://core.trac.wordpress.org/ticket/14802 )

Source

return apply_filters( 'pre_comment_approved', $approved, $comment_data );

Changelog

Version Description
4.9.0 Returning a WP_Error value from the filter will short-circuit comment insertion and allow skipping further processing.
2.1.0 Introduced.

User Contributed Notes