Using WordPress ‘is_avatar_comment_type()’ PHP function

The is_avatar_comment_type() WordPress PHP function checks if the given comment type allows avatars to be retrieved.

Usage

is_avatar_comment_type($comment_type);

Example:

if (is_avatar_comment_type('comment')) {
    echo "Avatars can be retrieved for this comment type.";
} else {
    echo "Avatars cannot be retrieved for this comment type.";
}

Parameters

  • $comment_type (string) – Required. Comment type to check.

More information

See WordPress Developer Resources: is_avatar_comment_type()

Examples

Checking if avatars are allowed for a specific comment type

This example checks if avatars are allowed for the ‘review’ comment type.

$comment_type = 'review';

if (is_avatar_comment_type($comment_type)) {
    echo "Avatars can be retrieved for the '$comment_type' comment type.";
} else {
    echo "Avatars cannot be retrieved for the '$comment_type' comment type.";
}

Displaying avatars for allowed comment types

This example displays the avatars for allowed comment types in a comments loop.

// In a comments loop
if (is_avatar_comment_type($comment->comment_type)) {
    echo get_avatar($comment->comment_author_email, 32);
}

Custom comment type with avatars

This example registers a custom comment type ‘product_review’ and allows avatars to be retrieved.

add_filter('avatar_comment_types', 'add_custom_avatar_comment_type');

function add_custom_avatar_comment_type($types) {
    $types[] = 'product_review';
    return $types;
}

Checking multiple comment types for avatars

This example checks if avatars are allowed for multiple comment types.

$comment_types = array('comment', 'review', 'testimonial');

foreach ($comment_types as $type) {
    if (is_avatar_comment_type($type)) {
        echo "Avatars can be retrieved for the '$type' comment type.<br>";
    } else {
        echo "Avatars cannot be retrieved for the '$type' comment type.<br>";
    }
}

Disabling avatars for default comment types

This example disables avatars for the default ‘comment’ type.

add_filter('avatar_comment_types', 'disable_avatars_for_default_comments');

function disable_avatars_for_default_comments($types) {
    if (($key = array_search('comment', $types)) !== false) {
        unset($types[$key]);
    }
    return $types;
}