bp_core_fetch_avatar_filter

Replace default WordPress avatars with BP avatars, if available.

Description

(string) bp_core_fetch_avatar_filter( (string) $avatar, (int|string|object) $user, (int) $size, (string) $default, (string) $alt = '', (array) $args = array() ); 

See get_avatar filter description in wp-includes/pluggable.php.

Returns (string)

BP avatar path, if found; else the original avatar path.

Parameters (6)

0. $avatar (string)
The avatar path passed to get_avatar..
1. $user (int|string|object)
A user ID, email address, or comment object.
2. $size (int)
Size of the avatar image ('thumb' or full).
3. $default (string)
URL to a default image to use if no avatar is available.
4. $alt — Optional. (string) => ''
Alternate text to use in image tag. Default: ''.
5. $args — Optional. (array) => array()
Arguments passed to get_avatar_data(), after processing.

Usage

  1. if ( !function_exists( 'bp_core_fetch_avatar_filter' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-avatars.php'; 
  3.  
  4. // The avatar path passed to 'get_avatar'. 
  5. $avatar = ''; 
  6.  
  7. // A user ID, email address, or comment object. 
  8. $user = null; 
  9.  
  10. // Size of the avatar image ('thumb' or 'full'). 
  11. $size = -1; 
  12.  
  13. // URL to a default image to use if no avatar is available. 
  14. $default = ''; 
  15.  
  16. // Alternate text to use in image tag. Default: ''. 
  17. $alt = ''; 
  18.  
  19. // Arguments passed to get_avatar_data(), after processing. 
  20. $args = array(); 
  21.  
  22. // NOTICE! Understand what this does before running. 
  23. $result = bp_core_fetch_avatar_filter($avatar, $user, $size, $default, $alt, $args); 
  24.  

Defined (1)

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

/bp-core/bp-core-avatars.php  
  1. function bp_core_fetch_avatar_filter( $avatar, $user, $size, $default, $alt = '', $args = array() ) { 
  2. global $pagenow; 
  3.  
  4. // Don't filter if inside WordPress options page and force_default is true. 
  5. if ( 'options-discussion.php' === $pagenow && true === $args['force_default'] ) { 
  6. return $avatar; 
  7.  
  8. // If passed an object, assume $user->user_id. 
  9. if ( is_object( $user ) ) { 
  10. if ( isset( $user->user_id ) ) { 
  11. $id = $user->user_id; 
  12. } else { 
  13. $id = $user->ID; 
  14.  
  15. // If passed a number, assume it was a $user_id. 
  16. } elseif ( is_numeric( $user ) ) { 
  17. $id = $user; 
  18.  
  19. // If passed a string and that string returns a user, get the $id. 
  20. } elseif ( is_string( $user ) && ( $user_by_email = get_user_by( 'email', $user ) ) ) { 
  21. $id = $user_by_email->ID; 
  22.  
  23. // If somehow $id hasn't been assigned, return the result of get_avatar. 
  24. if ( empty( $id ) ) { 
  25. return !empty( $avatar ) ? $avatar : $default; 
  26.  
  27. // Image alt tag. 
  28. if ( empty( $alt ) ) { 
  29. $alt = sprintf( __( 'Profile photo of %s', buddypress ), bp_core_get_user_displayname( $id ) ); 
  30.  
  31. // Use the 'thumb' type, unless the requested width is bigger than 
  32. // BP's thumb width. 
  33. $type = 'thumb'; 
  34. if ( (int) $size > bp_core_avatar_thumb_width() ) { 
  35. $type = 'full'; 
  36.  
  37. $avatar_args = array( 
  38. 'item_id' => $id,  
  39. 'type' => $type,  
  40. 'width' => $size,  
  41. 'height' => $size,  
  42. 'alt' => $alt,  
  43. ); 
  44.  
  45. // Support new arguments as of WordPress 4.2.0. 
  46. if ( ! empty( $args['width'] ) ) { 
  47. $avatar_args['width'] = $args['width']; 
  48. if ( ! empty( $args['height'] ) ) { 
  49. $avatar_args['height'] = $args['height']; 
  50. if ( ! empty( $args['class'] ) ) { 
  51. $avatar_args['class'] = $args['class']; 
  52. if ( ! empty( $args['class'] ) ) { 
  53. $avatar_args['class'] = $args['class']; 
  54. if ( ! empty( $args['extra_attr'] ) ) { 
  55. $avatar_args['extra_attr'] = $args['extra_attr']; 
  56. if ( ! empty( $args['scheme'] ) ) { 
  57. $avatar_args['scheme'] = $args['scheme']; 
  58. if ( ! empty( $args['force_default'] ) ) { 
  59. $avatar_args['force_default'] = $args['force_default']; 
  60. if ( ! empty( $args['rating'] ) ) { 
  61. $avatar_args['rating'] = $args['rating']; 
  62.  
  63. // Let BuddyPress handle the fetching of the avatar. 
  64. $bp_avatar = bp_core_fetch_avatar( $avatar_args ); 
  65.  
  66. // If BuddyPress found an avatar, use it. If not, use the result of get_avatar. 
  67. return ( !$bp_avatar ) ? $avatar : $bp_avatar;