Using WordPress ‘get_comment_type’ PHP filter

The get_comment_type WordPress PHP filter allows you to modify the returned comment type in WordPress.

Usage

add_filter('get_comment_type', 'your_custom_function', 10, 3);
function your_custom_function($comment_type, $comment_id, $comment) {
    // your custom code here
    return $comment_type;
}

Parameters

  • $comment_type (string): The type of comment, such as ‘comment’, ‘pingback’, or ‘trackback’.
  • $comment_id (string): The comment ID as a numeric string.
  • $comment (WP_Comment): The comment object.

More information

See WordPress Developer Resources: get_comment_type

Examples

Change Comment Type

Modify the comment type for specific comments.

add_filter('get_comment_type', 'change_specific_comment_type', 10, 3);
function change_specific_comment_type($comment_type, $comment_id, $comment) {
    // Change comment type for comment with ID 42 to 'pingback'
    if ($comment_id == '42') {
        $comment_type = 'pingback';
    }
    return $comment_type;
}

Add Custom Comment Type

Create a custom comment type based on a comment meta value.

add_filter('get_comment_type', 'add_custom_comment_type', 10, 3);
function add_custom_comment_type($comment_type, $comment_id, $comment) {
    // Check if comment has 'review' meta value
    $is_review = get_comment_meta($comment_id, 'review', true);
    if ($is_review) {
        $comment_type = 'review';
    }
    return $comment_type;
}

Replace Pingbacks with Trackbacks

Change all pingback comment types to trackbacks.

add_filter('get_comment_type', 'replace_pingback_with_trackback', 10, 3);
function replace_pingback_with_trackback($comment_type, $comment_id, $comment) {
    if ($comment_type == 'pingback') {
        $comment_type = 'trackback';
    }
    return $comment_type;
}

Filter Comment Types Based on User Role

Change comment types for users with a specific role.

add_filter('get_comment_type', 'filter_comment_type_by_user_role', 10, 3);
function filter_comment_type_by_user_role($comment_type, $comment_id, $comment) {
    // Get the comment author's user object
    $user = get_user_by('email', $comment->comment_author_email);

    // Check if the user has the 'editor' role
    if (in_array('editor', $user->roles)) {
        $comment_type = 'editor_comment';
    }
    return $comment_type;
}

Remove Comment Type for Anonymous Users

Remove comment types for anonymous users (not logged in).

add_filter('get_comment_type', 'remove_comment_type_for_anonymous_users', 10, 3);
function remove_comment_type_for_anonymous_users($comment_type, $comment_id, $comment) {
    // Check if the user is logged in
    if (empty($comment->user_id)) {
        $comment_type = '';
    }
    return $comment_type;
}