0

Would it be possible to use/allocate a font awesome icon as a user's avatar?

1

Yes, it would.

You can use get_avatar filter and display whatever you want as avatar. Here's an example:

function my_custom_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
    $user = false;

    if ( is_numeric( $id_or_email ) ) {

        $id = (int) $id_or_email;
        $user = get_user_by( 'id' , $id );

    } elseif ( is_object( $id_or_email ) ) {

        if ( ! empty( $id_or_email->user_id ) ) {
            $id = (int) $id_or_email->user_id;
            $user = get_user_by( 'id' , $id );
        }

    } else {
        $user = get_user_by( 'email', $id_or_email );   
    }

    if ( $user && is_object( $user ) ) {

        if ( $user->data->ID == '1' ) {
            $avatar = '<i class="fa fa-user" aria-hidden="true"></i>';
        }

    }

    return $avatar;
}
add_filter( 'get_avatar', 'my_custom_avatar', 10, 5 );

It will show user icon as avatar for user with ID == 1.

Of course it would make much more sense, if the icon was stored as some user meta and so on. But this should be some start...

  • How would I add multiple users and their icons to this? – Pete Dec 3 '18 at 10:10
  • @Pete it is fired up for every user. The if ( $user->data->ID == '1' ) { checks if it is user with ID = 1 and modifies only his avatar. So you have to change this part of code to whatever you need. – Krzysiek Dróżdż Dec 3 '18 at 10:24
  • OK but I want multiple users to keep their icon then I need multiple instances of the above code? – Pete Dec 3 '18 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.