The duplicate_comment_id WordPress PHP filter allows you to filter the ID of a duplicate comment found when creating a new comment.
Usage
add_filter('duplicate_comment_id', 'your_custom_function', 10, 2); function your_custom_function($dupe_id, $commentdata) { // your custom code here return $dupe_id; }
Parameters
$dupe_id
(int): ID of the comment identified as a duplicate.$commentdata
(array): Data for the comment being created.
More information
See WordPress Developer Resources: duplicate_comment_id
Examples
Allow duplicate comments
Prevent WordPress from blocking duplicate comments by returning an empty value.
add_filter('duplicate_comment_id', 'allow_duplicate_comments', 10, 2); function allow_duplicate_comments($dupe_id, $commentdata) { return ''; }
Allow duplicate comments from logged-in users
Allow logged-in users to post duplicate comments.
add_filter('duplicate_comment_id', 'allow_duplicate_comments_for_logged_in_users', 10, 2); function allow_duplicate_comments_for_logged_in_users($dupe_id, $commentdata) { if (is_user_logged_in()) { return ''; } return $dupe_id; }
Block duplicate comments with custom error message
Display a custom error message when a duplicate comment is detected.
add_filter('duplicate_comment_id', 'custom_duplicate_comment_message', 10, 2); function custom_duplicate_comment_message($dupe_id, $commentdata) { if ($dupe_id) { wp_die('Your comment appears to be a duplicate. Please try again.'); } return $dupe_id; }
Allow duplicate comments for specific post types
Allow duplicate comments for a custom post type, such as ‘product’.
add_filter('duplicate_comment_id', 'allow_duplicate_comments_for_products', 10, 2); function allow_duplicate_comments_for_products($dupe_id, $commentdata) { $post_type = get_post_type($commentdata['comment_post_ID']); if ($post_type === 'product') { return ''; } return $dupe_id; }
Block duplicate comments within a specific time frame
Prevent users from posting duplicate comments within a 5-minute time frame.
add_filter('duplicate_comment_id', 'block_duplicate_comments_within_time_frame', 10, 2); function block_duplicate_comments_within_time_frame($dupe_id, $commentdata) { if ($dupe_id) { $old_comment = get_comment($dupe_id); $time_diff = time() - strtotime($old_comment->comment_date); if ($time_diff < 300) { // 5 minutes in seconds wp_die('You cannot post a duplicate comment within 5 minutes. Please try again later.'); } } return $dupe_id; }