Support » Fixing WordPress » How to change avatar depending on the user's role

  • So far i found this
    https://wordpress.org/support/topic/how-to-change-the-default-mystery-man-gravatar?replies=11
    but I would like to replace the avatar depending on the user’s role, not simply the default one.
    this doesn’t work

    function set_owngravatar() {
    
        $avatarpath = 'http://website.com/images/';
        $user = wp_get_current_user();  
    
        if ( in_array( 'administrator', (array) $user->roles ) ) {
            $ownavatar = $avatarpath.'admin.jpg';
        }
        elseif ( in_array( 'editor', (array) $user->roles ) ) {
            $ownavatar = $avatarpath.'editor.jpg';
        }
        else { //logged out
            $ownavatar = $avatarpath.'anonymous-user.jpg';
        }
        return $ownavatar ;
    }
    
    //https://wordpress.org/support/topic/how-to-change-the-default-mystery-man-gravatar?replies=11
    add_filter( 'avatar_defaults', 'newgravatar' );
    function newgravatar ($avatar_defaults) {
        $myavatar = set_owngravatar();
        $avatar_defaults[$myavatar] = "Own";
        return $avatar_defaults;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to change avatar depending on the user's role’ is closed to new replies.