钩子文档

comment_flood_filter

💡 云策文档标注

概述

comment_flood_filter 是一个 WordPress 过滤器,用于控制评论洪水检测的状态。它允许开发者自定义评论洪水检查的逻辑,例如修改默认时间间隔或完全禁用洪水检测。

关键要点

  • comment_flood_filter 过滤器用于过滤评论洪水状态,参数包括布尔值(是否发生洪水)和两个时间戳(上次评论和新评论的时间)。
  • 默认情况下,WordPress 设置同一用户/IP 的评论间隔为 15 秒,超过此限制会触发洪水消息。
  • 开发者可以通过此过滤器修改洪水检测行为,例如调整时间间隔或禁用检测。

代码示例

// 修改默认评论洪水时间间隔为 30 秒
function wpdocs_dam_the_flood( $dam_it, $time_last, $time_new ) {
    if ( ( $time_new - $time_last ) < 30 ) {
        return true;
    }
    return false;
}
add_filter( 'comment_flood_filter', 'wpdocs_dam_the_flood', 10, 3 );

注意事项

  • 使用 add_filter( 'comment_flood_filter', '__return_false' ) 可以完全禁用评论洪水检测。
  • 此过滤器在 wp_check_comment_flood() 函数中被调用,用于检查评论洪水是否发生。
  • 自 WordPress 2.1.0 版本引入,开发者应确保代码兼容性。

📄 原文内容

Filters the comment flood status.

Parameters

$boolbool
Whether a comment flood is occurring. Default false.
$time_lastcommentint
Timestamp of when the last comment was posted.
$time_newcommentint
Timestamp of when the new comment was posted.

Source

$flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment );

Changelog

Version Description
2.1.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    The default time required between multiple comments from the same user/IP is 15 seconds. If the user posts the second comment faster than 15 seconds after the first comment, the comment flood message is triggered.
    Below is an example showing how to change the default time.
    Source: https://wordpress.org/support/article/faq-working-with-wordpress/#how-do-i-prevent-comment-flooding

    function wpdocs_dam_the_flood( $dam_it, $time_last, $time_new ) {
        if ( ( $time_new - $time_last ) < 300 ) { // time interval is 300 seconds
            return true;
        }
    
        return false;
    }
    add_filter( 'comment_flood_filter', 'wpdocs_dam_the_flood', 10, 3 );