0

I've tried a couple different wordpress plugins and solutions but nothing is quite what I'm looking for.

I want to adjust my theme/wordpress so that avatars for my Wordpress commenters are grabbed from the "website URL" that people fill in. Does anyone know how I would go about doing this?

2
  • 1
    What image exactly would it pull from these websites? (Or will you expect commenters to enter an URL pointing to a specific image file in the "website url" field?)
    – Iceable
    Commented Aug 7, 2018 at 11:12
  • I will expect them to enter an URL and pull a specific image. So they would submit their username, email, and then url as www.example.com/me.jpg and then me.jpg would be pulled as their avatar. Commented Aug 7, 2018 at 11:32

1 Answer 1

0

By default most of the time WordPress comments will include a "gravatar" (linked to the user's email address) when displaying comments, using WordPress built-in function get_avatar().

You can hook a custom function to the get_avatar filter to return another image.

Something like this should do the trick for your use case: it filters 'get_avatar' and returns an image with the comment author's website URL if it is set. Add this to your theme (or child theme) functions.php:

add_filter( 'get_avatar', 'wpse310726_custom_avatar', 1, 5 );

function wpse310726_custom_avatar( $avatar, $id_or_email, $size, $default, $alt = null ) {

    $comment_author_url = get_comment_author_url();

    if ( '' !== $comment_author_url ) {
        $avatar = "<img alt='{$alt}' src='{$comment_author_url}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
    }

    return $avatar;
}
4
  • This almost works - I see avatar nows, but I'm getting "Warning: Missing argument 5 for wpse310726_custom_avatar() in /home/qpwdlrrp/website.com/functions.php on line 90" for the usernames of the commenters. Commented Aug 7, 2018 at 12:17
  • The 5th argument being optional, setting a default value of null in case in it not passed to the filter should fix it. I edited my answer. (By the way: once you are done working on your site don't forget to set WP_DEBUG to false in wp-config.php on your live website)
    – Iceable
    Commented Aug 7, 2018 at 12:24
  • Thank you very much, it works now! What is the meaning behind the WP_Debug change? Just curious, not very good at coding Commented Aug 7, 2018 at 13:00
  • You want 'WP_DEBUG' to be set to true during development, so every php error, warning and notice are displayed and you can improve your code as appropriate. Once you've chased and fixed every bug no error should be left to display, but in case one comes up at any point you don't want it to be displayed on a production (live, publicly viewable) site. Not only it wouldn't look good, but it could giveaway sensitive information to potential attackers. See also: codex.wordpress.org/Debugging_in_WordPress
    – Iceable
    Commented Aug 7, 2018 at 13:11

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.