The get_comment_ID WordPress PHP filter allows you to modify the comment ID before it’s returned.
Usage
add_filter('get_comment_ID', 'your_custom_function', 10, 2);
function your_custom_function($comment_id, $comment) {
// your custom code here
return $comment_id;
}
Parameters
- $comment_id (string) – The current comment ID as a numeric string.
- $comment (WP_Comment) – The comment object.
More information
See WordPress Developer Resources: get_comment_ID
Examples
Change comment ID
Modify the comment ID by adding a prefix to it.
add_filter('get_comment_ID', 'prefix_comment_id', 10, 2);
function prefix_comment_id($comment_id, $comment) {
$new_comment_id = 'myprefix_' . $comment_id;
return $new_comment_id;
}
Hide specific comment ID
Hide a specific comment ID by returning an empty string for that ID.
add_filter('get_comment_ID', 'hide_specific_comment_id', 10, 2);
function hide_specific_comment_id($comment_id, $comment) {
if ($comment_id == '123') {
return '';
}
return $comment_id;
}
Change comment ID based on user role
Modify the comment ID based on the user role of the comment author.
add_filter('get_comment_ID', 'change_id_based_on_user_role', 10, 2);
function change_id_based_on_user_role($comment_id, $comment) {
$user = new WP_User($comment->user_id);
if (in_array('administrator', $user->roles)) {
return 'admin_' . $comment_id;
}
return $comment_id;
}
Append post ID to comment ID
Append the post ID to the comment ID.
add_filter('get_comment_ID', 'append_post_id_to_comment_id', 10, 2);
function append_post_id_to_comment_id($comment_id, $comment) {
$post_id = $comment->comment_post_ID;
return $comment_id . '_post_' . $post_id;
}
Add custom ID for anonymous comments
Add a custom ID for anonymous comments.
add_filter('get_comment_ID', 'custom_id_for_anonymous_comments', 10, 2);
function custom_id_for_anonymous_comments($comment_id, $comment) {
if ($comment->user_id == 0) {
return 'anonymous_' . $comment_id;
}
return $comment_id;
}