0

I have always used the get_avatar_url filter to alter the user avatar where it's printed.

add_filter( 'get_avatar_url', 'my\plugin\filter_get_avatar_url', 10, 3 );

function filter_get_avatar_url( $url, $id_or_email, $args ) {

   // Do something and return whatever I want

}

But now I am printing comments with the wp_list_comments() function. And it's getting the avatar from Gravatar.

I expect it to respect the filter. What should I do?

3 Answers 3

1

By default, wp_list_comments() uses the get_avatar() function to display user avatars, which in turn retrieves the avatar URL from Gravatar. However, get_avatar() also applies the get_avatar_url filter before returning the URL, so you can modify the avatar URL for the comments by using that filter.

Here's an example of how you can modify the avatar URL for comments using the get_avatar_url filter:

add_filter( 'get_avatar_url', 'my_plugin_filter_get_avatar_url', 10, 3 );
function my_plugin_filter_get_avatar_url( $url, $id_or_email, $args ) {
    // Check if we are displaying a comment avatar
    if ( is_object( $args ) && isset( $args->object_type ) && $args->object_type === 'comment' ) {
        // Do something to modify the avatar URL for comments
        $modified_url = 'https://example.com/my-modified-avatar-url.jpg';
        return $modified_url;
    }
    // Return the original avatar URL for other cases
    return $url;
}

In this example, we check if the object_type argument of the $args parameter is set to 'comment', which indicates that we are displaying a comment avatar. If that's the case, we modify the avatar URL as desired, and return the modified URL. For other cases, we simply return the original URL.

Note that if you have other filters hooked to the get_avatar_url filter, they will also be applied to the comment avatars, so make sure your filter is compatible with other filters that may be running.

1

The get_avatar_url filter you are using only affects the avatar URL generated by the get_avatar_url function. The wp_list_comments function, on the other hand, uses the get_avatar function to generate the avatar HTML, which does not go through the get_avatar_url filter.

To modify the avatar displayed in comments, you can use the get_avatar_data filter, which is called by the get_avatar function and allows you to modify the data used to generate the avatar HTML. Here's an example of how you can use it:

add_filter( 'get_avatar_data', 'my\plugin\filter_get_avatar_data', 10, 2 );

function filter_get_avatar_data( $args, $id_or_email ) {
   // Modify the avatar data as needed
   $args['url'] = 'https://example.com/my-custom-avatar.png';

   return $args;
}

In this example, the get_avatar_data filter is used to modify the avatar data by changing the url parameter to the URL of a custom avatar image. You can modify the avatar data in any way you need, and the modified data will be used to generate the avatar HTML.

Note that the get_avatar_data filter is called with two parameters: $args, which is an array of arguments used to generate the avatar, and $id_or_email, which is the user ID or email address associated with the avatar. You can use these parameters to modify the avatar data as needed.

Keep in mind that modifying the avatar HTML in comments can be tricky, as comments may be cached by WordPress or by third-party caching plugins, so your changes may not be immediately visible. Additionally, modifying the avatar data may also affect other parts of your site that use the get_avatar function, so be sure to test thoroughly.

Reference: https://developer.wordpress.org/reference/hooks/get_avatar_data/

0

After researching a bit more, I found out that we can indeed get the right avatar with the filter. The problem is, the filter param $id_or_email accepts many values: a user ID, Gravatar MD5 hash, user email, WP_User object, WP_Post object, or WP_Comment object.

So, I had to modify the function that filters the value and make sure it also handles the case that the passed value is an instance of WP_Comment.

function filter_get_avatar_url( $url, $id_or_email ) {
    
    // If $id_or_email is a WP_User object, just get the user ID from it
    if ( is_object( $id_or_email ) && isset( $id_or_email->ID ) ) {
        $id_or_email = $id_or_email->ID;
    }

    // If $id_or_email is a WP_Comment object, just get the user ID from it
    if ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) ) {
        $id_or_email = $id_or_email->user_id;
    }

    // Normalize the current user ID
    if ( is_numeric( $id_or_email ) ) {
        $user_id = $id_or_email;
    } else if ( is_string( $id_or_email ) && is_email( $id_or_email ) ) {
        $user = get_user_by( 'email', $id_or_email );
        if ( empty( $user ) ) {
            return $url;
        }
        $user_id = $user->ID;
    } else {
        return $url;
    }

    // Get user meta avatar
    $custom_avatar_url = get_user_meta( $user_id, 'avatar', true );

    if ( empty( $custom_avatar_url ) ) {
        return $url;
    }

    return $custom_avatar_url;

}

Now, it's working as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.