Using WordPress ‘comment_duplicate_message’ PHP filter

The comment_duplicate_message WordPress PHP filter allows you to modify the duplicate comment error message displayed to users when they submit a duplicate comment.

Usage

add_filter('comment_duplicate_message', 'your_function_name');
function your_function_name($comment_duplicate_message) {
  // your custom code here
  return $comment_duplicate_message;
}

Parameters

  • $comment_duplicate_message (string): The original duplicate comment error message.

More information

See WordPress Developer Resources: comment_duplicate_message

Examples

Change the error message

Change the duplicate comment error message to a custom message.

add_filter('comment_duplicate_message', 'custom_duplicate_message');
function custom_duplicate_message($comment_duplicate_message) {
  $comment_duplicate_message = 'Please do not submit the same comment twice.';
  return $comment_duplicate_message;
}

Add a link to the comment policy page in the error message.

add_filter('comment_duplicate_message', 'add_policy_link');
function add_policy_link($comment_duplicate_message) {
  $comment_duplicate_message .= ' Please review our <a href="/comment-policy">comment policy</a>.';
  return $comment_duplicate_message;
}

Add an emoji to the message

Add an emoji at the beginning of the error message.

add_filter('comment_duplicate_message', 'add_emoji');
function add_emoji($comment_duplicate_message) {
  $comment_duplicate_message = '😅 ' . $comment_duplicate_message;
  return $comment_duplicate_message;
}

Display the message in uppercase

Display the error message in uppercase letters.

add_filter('comment_duplicate_message', 'uppercase_message');
function uppercase_message($comment_duplicate_message) {
  $comment_duplicate_message = strtoupper($comment_duplicate_message);
  return $comment_duplicate_message;
}

Add a prefix to the message

Add a prefix “Error: ” to the error message.

add_filter('comment_duplicate_message', 'add_prefix');
function add_prefix($comment_duplicate_message) {
  $comment_duplicate_message = 'Error: ' . $comment_duplicate_message;
  return $comment_duplicate_message;
}