All the comments on my website are anonymous and without registration. To post a comment you have to fill the fields name (not obligatorily) and the text of your comment (obligatorily).
I have the problem with the avatars. All of them are generated the same.
What should I do to make them different?
There are two ways to customize the default avatar:
get_avatar()
.Letâs start with the first option; this processes slightly faster.
Add a new default avatar to Settings/Discussion
There is a filter
'avatar_defaults'
. You can add more avatars here.You get an array of default images where the key is an URL and the value the visible name.
Sample Code
Result
Change the output of
get_avatar()
.get_avatar()
searches in an option named'avatar_default'
first. We can hook into'pre_option_avatar_default'
and return a custom (random) URL.Sample code
Result
As you can see â you should return an image with a size that matches your themeâs avatar size. 🙂
Re: random avatar generation, this was an extremely helpful answer and worked a treat with one minor modification – the function in question required an argument to pass through. Without the argument, the code returned included a jumble of html which included both the desired url of the image, as well as url of the default icon from gravitar. I changed it from this:
wpse_58373_custom_default_avatar
()to this:
and that seemed to generate the image url without the jumble. For ease of use, the final code, with the addition of get_template_directory_uri () to target the theme rather than the admin folder, and some tucking away of variables was:
Hope to allow users to choose their own soon, but for those who just want to comment, it keeps the look of the site consistent.
To add to what already has been said, I suggest using the plugin “code snippets”
This way, your modifications to the internals of wordpress will not collide with updates, and you can actually manage them (activate, deactivate, export them… )
I set up a little tutorial to create custom random avatars using info from this question and Code Snippets
custom random avatars tutorial
This is how it looks
The code I used is pretty simple