The comment_flood_trigger WordPress PHP action fires before the comment flood message is triggered. It helps in managing the time between consecutive comments from the same user.
Usage
add_action('comment_flood_trigger', 'your_custom_function', 10, 2); function your_custom_function($time_lastcomment, $time_newcomment) { // Your custom code here }
Parameters
$time_lastcomment
(int) – Timestamp of when the last comment was posted.$time_newcomment
(int) – Timestamp of when the new comment was posted.
More information
See WordPress Developer Resources: comment_flood_trigger
Examples
Change the flood interval
Modify the minimum time interval between consecutive comments.
add_action('comment_flood_trigger', 'change_flood_interval', 10, 2); function change_flood_interval($time_lastcomment, $time_newcomment) { // Set the custom flood interval in seconds $custom_interval = 60; if (($time_newcomment - $time_lastcomment) < $custom_interval) { wp_die('Please wait before posting another comment.'); } }
Log comment flood attempts
Log comment flood attempts for further analysis.
add_action('comment_flood_trigger', 'log_comment_flood_attempts', 10, 2); function log_comment_flood_attempts($time_lastcomment, $time_newcomment) { // Log the flood attempt details error_log("Comment flood attempt: Last comment at {$time_lastcomment}, new comment at {$time_newcomment}"); }
Notify admin of comment flood attempts
Send an email notification to the admin when a comment flood attempt is detected.
add_action('comment_flood_trigger', 'notify_admin_flood_attempt', 10, 2); function notify_admin_flood_attempt($time_lastcomment, $time_newcomment) { $admin_email = get_option('admin_email'); $subject = 'Comment flood attempt detected'; $message = "A comment flood attempt was detected on your website. Last comment at {$time_lastcomment}, new comment at {$time_newcomment}."; wp_mail($admin_email, $subject, $message); }
Display a custom message for comment flood attempts
Show a custom message to users when a comment flood attempt is detected.
add_action('comment_flood_trigger', 'custom_flood_message', 10, 2); function custom_flood_message($time_lastcomment, $time_newcomment) { wp_die('Please take a break before posting another comment.'); }
Disable comment flood check for certain user roles
Allow specific user roles to bypass the comment flood check.
add_action('comment_flood_trigger', 'disable_flood_check_for_roles', 10, 2); function disable_flood_check_for_roles($time_lastcomment, $time_newcomment) { $user = wp_get_current_user(); $allowed_roles = array('administrator', 'editor'); if (array_intersect($allowed_roles, $user->roles)) { return; } // Default comment flood check if (($time_newcomment - $time_lastcomment) < 15) { wp_die('Please wait before posting another comment.'); } }