Displaying random avatars for WordPress comments

Gravatar is configured by default in WordPress., which can pull up the photo of the author of the commentator by his e-mail. If the email address is not specified in the comments and the user was not registered in the gravatar service, then the standard not very beautiful gravatar icon is displayed.

What are the alternative options for customizing avatars:

  • You can customize in WordPress static avatars visitors, which will be used in all comments added by anonymous users.
  • You can also add generated avatars that will be different for each user.

Settings for standard avatars can be found in the Settings section > Discussion. You can also see these 2 kind of avatars in the image below.

Кастомные аватары в WordPress

In this tutorial, I wanted to show something similar to the standard generated, but more interesting. To change the standard avatars, you need to use the function get_avatar() exists 2 basic way.

  • You can add a filter, to parameter avatar_defaults and change the array of avatars, to get a new parameter for “static” for an option in Discussion Settings.
  • You can add a filter for pre_option_avatar_default and return default url.

I will use the second option, so the returned URL will be randomly selected from the set. Below is an example of a filter:

This filter will change the default WordPress flow and return a random avatar to the user. When commenting, the avatar will constantly change, even when the page is reloaded, but these will no longer be standard comments. Avatar files are in the site theme in the new_avatars folder and are selected randomly.

This is how the avatars look on my test site.

wordpress change avatar

This is the simplest implementation example showing how you can work with avatars on the site and the filter 'pre_option_avatar_default'’ in WordPress.


Buy WordPress hosting
WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal