âš¡Voxel Black Friday Sale is now Live

Custom Generated Avatars for Users

Description/Instructions

Sometimes users won’t know how to or won’t even upload an image to your platform for their profile. No worries as we can auto generate one for them. Using DiceBear, we can create a unique avatar for each user on account creation. You can even choose the style of the avatar generated for all users.

Download this code and paste it in your Child Theme functions.php file.

To change the Avatar type, go under Settings > Avatars

If this snippet helped, feel free to buy me a taco :)

/**

* 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:  http://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);

  • PHP
Copy Code

Let's Chat About this Snippet