Skip to content

Instantly share code, notes, and snippets.

Last active January 3, 2023 02:53
Show Gist options
  • Save khromov/57834d9c0b45aac122ce51bcce431679 to your computer and use it in GitHub Desktop.
Save khromov/57834d9c0b45aac122ce51bcce431679 to your computer and use it in GitHub Desktop.
DiceBear avatars for WordPress

This is an example implementation of DiceBear Avatars for WordPress comments.

You can see a demo for this plugin at the bottom of this post:


Aside from implementing the DiceBear Micah avatar style, this plugin also removes the website and email fields from the WordPress comments form. This means names are used for avatar generation for anonymous users, and user ID:s are used for logged in users.

You can use the official DiceBear CDN or self-host your own avatar server for increased privacy.


  • Activate the plugin
  • Go to Settings > Discussion > Default avatar and select "Dicebear: Micah"

You can further customize the plugin with constants:

Set a custom avatars server

You can deploy this via Docker using this image.


Set a custom salt for avatar generation

This ensures that if emails are used to generate avatars, it is impossible to reverse the emails. You can also change this if you ever want to reset all avatars.

define( 'DICEBEAR_SALT', '1234567' ); 
Plugin Name: Dicebear Avatars
* Automatically tick the "Save my data in this browser" checkbox on comments if it was ticked before.
add_action('wp_footer', function() {
if(is_single() && comments_open()):
if(document.cookie && document.cookie.includes('comment_author_')) {
document.getElementById('wp-comment-cookies-consent').checked = true;
* Add Dicebear avatar option to Settings > Discussion
add_filter('avatar_defaults', function($avatar_defaults) {
$new_avatar_defaults = $avatar_defaults;
$new_avatar_defaults['dicebear_micah'] = 'Dicebear: Micah';
return $new_avatar_defaults;
* Disable requiring user email/password.
* Unfortunately it's not possible to disable only email.
add_filter('pre_option_require_name_email', function($value) {
return "0";
* Disable Email and website fields
add_filter('comment_form_default_fields', function($fields) {;
if(isset($fields['url'])) {
if(isset($fields['email'])) {
// Not very clean but doesn't seem filterable otherwise.
$fields['cookies'] = str_replace('Save my name, email, and website in this browser for the next time I comment.', 'Save my name in this browser for the next time I comment.', $fields['cookies']);
return $fields;
* Replace the avatar URL.
* @param array $args Arguments passed to get_avatar_data(), after processing.
* @param mixed $data The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
* user email, WP_User object, WP_Post object, or WP_Comment object.
add_filter('pre_get_avatar_data', function($args, $data) {
$dicebear_avatars_base_url = '';
// Something "unique" for each site
$dicebear_salt = md5(get_site_url());
$dicebear_avatars_base_url = DICEBEAR_AVATARS_BASE_URL;
if(defined('DICEBEAR_SALT')) {
$dicebear_salt = DICEBEAR_SALT;
$seed = 'default';
if($data instanceof WP_Comment) {
$seed = md5($dicebear_salt . $data->comment_author); // If comment, use the name for a persistent hash
} else if($data instanceof WP_User) {
$seed = md5($dicebear_salt . $data->ID); // If user, use the user ID
} else if($data instanceof WP_Post) {
$seed = md5($dicebear_salt . $data->post_author); // Not sure what we are expecting here 🤷
} else if(is_int($data)) {
$seed = md5($dicebear_salt . $data);
} else if(is_string($seed)) {
$seed = md5($dicebear_salt . $data); // This one is most likely to be an email, but we can't control it.
$simple_local_avatar_url = "{$dicebear_avatars_base_url}/api/micah/{$seed}.svg?mouth[]=laughing&mouth[]=laughing&mouth[]=smile&mouth[]=pucker&mouth[]=smirk&baseColor[]=white";
if($args['default'] === 'dicebear_micah') {
$args['url'] = $simple_local_avatar_url;
return $args;
return $args;
}, 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment