bb_get_avatar

Retrieve the avatar for a user provided a user ID or email address.

Description

(string) bb_get_avatar( (int|string) $id_or_email, (int) $size = 80, (string) $default = '', (bool) $alt = false ); 

Returns (string)

tag for the user's avatar

Parameters (4)

0. $id_or_email (int|string)
A user ID or email address
1. $size — Optional. (int) => 80
Size of the avatar image
2. $default — Optional. (string) => ''
URL to a default image to use if no avatar is available
3. $alt — Optional. (bool) => false
Alternate text to use in image tag. Defaults to blank

Usage

  1. if ( !function_exists( 'bb_get_avatar' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-pluggable.php'; 
  3.  
  4. // A user ID or email address 
  5. $id_or_email = null; 
  6.  
  7. // Size of the avatar image 
  8. $size = 80; 
  9.  
  10. // URL to a default image to use if no avatar is available 
  11. $default = ''; 
  12.  
  13. // Alternate text to use in image tag. Defaults to blank 
  14. $alt = false; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = bb_get_avatar($id_or_email, $size, $default, $alt); 
  18.  

Defined (1)

The function is defined in the following location(s).

/bp-forums/bbpress/bb-includes/functions.bb-pluggable.php  
  1. function bb_get_avatar( $id_or_email, $size = 80, $default = '', $alt = false ) { 
  2. if ( !bb_get_option('avatars_show') ) 
  3. return false; 
  4.  
  5. if ( false === $alt) 
  6. $safe_alt = ''; 
  7. else 
  8. $safe_alt = esc_attr( $alt ); 
  9.  
  10. if ( !is_numeric($size) ) 
  11. $size = 80; 
  12.  
  13. if ( $email = bb_get_user_email($id_or_email) ) { 
  14. $class = 'photo '; 
  15. } else { 
  16. $class = ''; 
  17. $email = $id_or_email; 
  18.  
  19. if ( !$email ) 
  20. $email = ''; 
  21.  
  22. if ( empty($default) ) 
  23. $default = bb_get_option('avatars_default'); 
  24.  
  25. if ( is_ssl() ) 
  26. $host = 'https://secure.gravatar.com'; 
  27. else 
  28. $host = 'http://www.gravatar.com'; 
  29.  
  30. switch ($default) { 
  31. case 'logo': 
  32. $default = ''; 
  33. break; 
  34. case 'blank': 
  35. $default = bb_get_uri( 'bb-admin/images/blank.gif', null, BB_URI_CONTEXT_IMG_SRC ); 
  36. break; 
  37. case 'monsterid': 
  38. case 'wavatar': 
  39. case 'identicon': 
  40. case 'retro': 
  41. break; 
  42. case 'default': 
  43. default: 
  44. $default = $host . '/avatar/ad516503a11cd5ca435acc9bb6523536?s=' . $size; 
  45. // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com') 
  46. break; 
  47.  
  48. $src = $host . '/avatar/'; 
  49. $class .= 'avatar avatar-' . $size; 
  50.  
  51. if ( !empty($email) ) { 
  52. $src .= md5( strtolower( $email ) ); 
  53. } else { 
  54. $src .= 'd41d8cd98f00b204e9800998ecf8427e'; 
  55. // d41d8cd98f00b204e9800998ecf8427e == md5('') 
  56. $class .= ' avatar-noemail'; 
  57.  
  58. $src .= '?s=' . $size; 
  59. $src .= '&d=' . urlencode( $default ); 
  60.  
  61. $rating = bb_get_option('avatars_rating'); 
  62. if ( !empty( $rating ) ) 
  63. $src .= '&r=' . $rating; 
  64.  
  65. $avatar = '<img alt="' . $safe_alt . '" src="' . $src . '" class="' . $class . '" style="height:' . $size . 'px; width:' . $size . 'px;" />'; 
  66.  
  67. return apply_filters('bb_get_avatar', $avatar, $id_or_email, $size, $default, $alt);