Snippets / PHP
Custom Generated Avatars for Users
Download this code and paste it in your Child Theme functions.php file.
To change the Avatar type, go under Settings > Avatars
/**
* Plugin Name: Avatars
* Plugin URI: https://voxel.guide
* Description: Customize user avatars using DiceBear Avatars with selectable styles.
* Version: 1.0
* Author: Voxel Guide
* Author URI: https://voxel.guide
*/
add_action('admin_menu', 'avatars_add_admin_menu');
add_action('admin_init', 'avatars_settings_init');
function avatars_add_admin_menu() {
add_options_page('Avatars', 'Avatars', 'manage_options', 'avatars', 'avatars_options_page');
}
function avatars_settings_init() {
register_setting('pluginPage', 'avatars_settings');
add_settings_section(
'avatars_pluginPage_section',
__('Choose your default avatar style for new users.', 'avatars'),
'avatars_settings_section_callback',
'pluginPage'
);
add_settings_field(
'avatars_select_field_0',
__('Avatar Style', 'avatars'),
'avatars_select_field_0_render',
'pluginPage',
'avatars_pluginPage_section'
);
}
function avatars_select_field_0_render() {
$options = get_option('avatars_settings');
?>
<select name='avatars_settings[avatars_select_field_0]'>
<option value='adventurer' <?php selected($options['avatars_select_field_0'], 'adventurer'); ?>>Adventurer</option>
<option value='adventurer-neutral' <?php selected($options['avatars_select_field_0'], 'adventurer-neutral'); ?>>Adventurer Neutral</option>
<option value='avataaars' <?php selected($options['avatars_select_field_0'], 'avataaars'); ?>>Avataaars</option>
<option value='avataaars-neutral' <?php selected($options['avatars_select_field_0'], 'avataaars-neutral'); ?>>Avataaars Neutral</option>
<option value='big-ears' <?php selected($options['avatars_select_field_0'], 'big-ears'); ?>>Big Ears</option>
<option value='big-ears-neutral' <?php selected($options['avatars_select_field_0'], 'big-ears-neutral'); ?>>Big Ears Neutral</option>
<option value='big-smile' <?php selected($options['avatars_select_field_0'], 'big-smile'); ?>>Big Smile</option>
<option value='bottts' <?php selected($options['avatars_select_field_0'], 'bottts'); ?>>Bottts</option>
<option value='bottts-neutral' <?php selected($options['avatars_select_field_0'], 'bottts-neutral'); ?>>Bottts Neutral</option>
<option value='croodles' <?php selected($options['avatars_select_field_0'], 'croodles'); ?>>Croodles</option>
<option value='croodles-neutral' <?php selected($options['avatars_select_field_0'], 'croodles-neutral'); ?>>Croodles Neutral</option>
<option value='fun-emoji' <?php selected($options['avatars_select_field_0'], 'fun-emoji'); ?>>Fun Emoji</option>
<option value='icons' <?php selected($options['avatars_select_field_0'], 'icons'); ?>>Icons</option>
<option value='identicon' <?php selected($options['avatars_select_field_0'], 'identicon'); ?>>Identicon</option>
<option value='initials' <?php selected($options['avatars_select_field_0'], 'initials'); ?>>Initials</option>
<option value='lorelei' <?php selected($options['avatars_select_field_0'], 'lorelei'); ?>>Lorelei</option>
<option value='lorelei-neutral' <?php selected($options['avatars_select_field_0'], 'lorelei-neutral'); ?>>Lorelei Neutral</option>
<option value='micah' <?php selected($options['avatars_select_field_0'], 'micah'); ?>>Micah</option>
<option value='miniavs' <?php selected($options['avatars_select_field_0'], 'miniavs'); ?>>Miniavs</option>
<option value='notionists' <?php selected($options['avatars_select_field_0'], 'notionists'); ?>>Notionists</option>
<option value='notionists-neutral' <?php selected($options['avatars_select_field_0'], 'notionists-neutral'); ?>>Notionists Neutral</option>
<option value='open-peeps' <?php selected($options['avatars_select_field_0'], 'open-peeps'); ?>>Open Peeps</option>
<option value='personas' <?php selected($options['avatars_select_field_0'], 'personas'); ?>>Personas</option>
<option value='pixel-art' <?php selected($options['avatars_select_field_0'], 'pixel-art'); ?>>Pixel Art</option>
<option value='pixel-art-neutral' <?php selected($options['avatars_select_field_0'], 'pixel-art-neutral'); ?>>Pixel Art Neutral</option>
<option value='rings' <?php selected($options['avatars_select_field_0'], 'rings'); ?>>Rings</option>
<option value='shapes' <?php selected($options['avatars_select_field_0'], 'shapes'); ?>>Shapes</option>
<option value='thumbs' <?php selected($options['avatars_select_field_0'], 'thumbs'); ?>>Thumbs</option>
</select>
<?php
}
function avatars_settings_section_callback() { }
function avatars_options_page() {
?>
<form action='options.php' method='post'>
<h2>Avatars</h2>
<?php
settings_fields('pluginPage');
do_settings_sections('pluginPage');
submit_button();
?>
</form>
<?php
}
function avatars_custom_dicebear_avatar($avatar, $id_or_email, $size, $default, $alt) {
$options = get_option('avatars_settings');
$avatar_style = isset($options['avatars_select_field_0']) ? $options['avatars_select_field_0'] : 'identicon';
if (is_numeric($id_or_email)) {
$user = get_user_by('id', $id_or_email);
$email = $user->user_email;
} elseif (is_object($id_or_email)) {
if (!empty($id_or_email->user_id)) {
$user = get_user_by('id', $id_or_email->user_id);
$email = $user->user_email;
} else {
$email = $id_or_email->comment_author_email;
}
} else {
$email = $id_or_email;
}
$seed = md5(strtolower(trim($email)));
$dicebear_url = "https://api.dicebear.com/7.x/{$avatar_style}/svg?seed=${seed}";
$avatar = "<img src='{$dicebear_url}' class='avatar avatar-${size} photo' height='{$size}' width='{$size}' alt='{$alt}' />";
return $avatar;
}
add_filter('get_avatar', 'avatars_custom_dicebear_avatar', 10, 5);
Discussion 3
Ask a question or share how you used this.
Hi Mr. Donald. When I save it in function.php, there is error : Your PHP code changes were not applied due to an error on line 189 of file wp-content/themes/voxel-child/functions.php. Please fix and try saving again. syntax error, unexpected identifier " " Line 189: add_options_page('Avatars', 'Avatars', 'manage_options', 'avatars', 'avatars_options_page'); Please check/ Thank you in advance.
Hey!. :) The code works fine, but i have a quick question. When a user upload a profile pic, the autogenerated is still shown. How can i disable it, if a user upload his own profile picture? Thanks. :)
The snippet isn't working for me, throwing an error for the strings at the end. Kind of looks like the formatting got messed up on it.
Good catch. Seems the text field is formatting it weird. Try this article here: https://voxel.guide/article/generate-random-avatars-for-users-on-account-creation/