Sometimes, when posting comments, duplicate comments appear (hosting is very busy ). im use add_filter('comment_flood_filter', '__return_false');
How to not display “Duplicate comment detected” but redirect to post? Without adding a duplicate comment to the database.
In other words – count only one comment, and if the same is found, then redirect to the post.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This answer lets you redirect back to the originating post instead of dieing with the flood message. It’s a PHP class, not just a simple function. You can copy-paste this into your functions.php as-is, or set it up in whichever way you like to manage your custom code.
- assumes PHP 5.3+
- it also contains code for duplicate comments which your original question mentioned, but then you edited it out. You can remove this bit by deleting the line that contains
add_action( 'comment_duplicate_trigger,…
class Handle_Comment_Flood {
private $comment_post_id;
public function __construct() {
add_filter( 'preprocess_comment', [ $this, 'capture_post_id' ], 10, 1 );
add_action( 'comment_flood_trigger', [ $this, 'handle_redirect' ], 0, 0 );
add_action( 'comment_duplicate_trigger', [ $this, 'handle_redirect' ], 0, 0 );
}
public function capture_post_id( $comment ) {
$this->comment_post_id = isset( $comment[ 'comment_post_ID' ] ) ? $comment[ 'comment_post_ID' ] : 0;
return $comment;
}
public function handle_redirect() {
if ( !empty( $this->comment_post_id ) ) {
wp_safe_redirect( get_permalink( get_post( $this->comment_post_id ) ) );
die();
}
}
}
new Handle_Comment_Flood();
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0