wp_throttle_comment_flood()WP 2.1.0

Gets the indication of whether a comment is blocked and the time of two comments to determine whether the new comment should be blocked due to being sent too quickly.

The function is used as a handler for the filter comment_flood_filter.

If the comment has already been blocked by another handler, the function leaves that decision unchanged. If less than 15 seconds have passed between the last and the new comment, the comment is considered comment flood.

This function is usually not called directly. It is needed as a standard callback to check frequent comment sending.

The interval of 15 seconds is hard-coded inside the function. To change it, you need to replace the filter handler comment_flood_filter with your own callback.

No Hooks.

Returns

true|false.

  • true — the comment should be blocked.
  • false — the comment does not need to be blocked.

Usage

wp_throttle_comment_flood( $block, $time_lastcomment, $time_newcomment );
$block(bool) (required)
Indicates whether the comment has already been blocked by another handler. If true is passed, the function will immediately return true and will not check the comment time.
$time_lastcomment(int) (required)
Unix timestamp of the last comment time.
$time_newcomment(int) (required)
Unix timestamp of the new comment time.

Examples

0

#1 Checking for comment flooding

Let's check whether a new comment will be blocked if it is submitted 10 seconds after the previous one.

$time_lastcomment = time();
$time_newcomment  = $time_lastcomment + 10;

$is_blocked = wp_throttle_comment_flood( false, $time_lastcomment, $time_newcomment );

if ( $is_blocked ) {
	echo 'Comment submitted too quickly.';
}
0

#2 The comment is not blocked if 15 seconds or more have passed

The function returns false if the difference between comments is 15 seconds or more.

$time_lastcomment = time();
$time_newcomment  = $time_lastcomment + 15;

$is_blocked = wp_throttle_comment_flood( false, $time_lastcomment, $time_newcomment );

if ( ! $is_blocked ) {
	echo 'The comment can be sent.';
}
0

#3 Replace the standard limit with 30 seconds

We’ll remove the default check and add our own, where comments are blocked if less than 30 seconds have passed between them.

add_action( 'plugins_loaded', 'wpdocs_replace_comment_flood_check' );

function wpdocs_replace_comment_flood_check() {
	remove_filter( 'comment_flood_filter', 'wp_throttle_comment_flood', 10 );

	add_filter( 'comment_flood_filter', 'wpdocs_throttle_comment_flood_30_seconds', 10, 3 );
}

function wpdocs_throttle_comment_flood_30_seconds( $block, $time_lastcomment, $time_newcomment ) {
	if ( $block ) {
		return $block;
	}

	return ( $time_newcomment - $time_lastcomment ) < 30;
}

Changelog

Since 2.1.0 Introduced.

wp_throttle_comment_flood() code WP 7.0.2

function wp_throttle_comment_flood( $block, $time_lastcomment, $time_newcomment ) {
	if ( $block ) { // A plugin has already blocked... we'll let that decision stand.
		return $block;
	}
	if ( ( $time_newcomment - $time_lastcomment ) < 15 ) {
		return true;
	}
	return false;
}