Wordpress avatar url


/ Published in: PHP
Save to your folder(s)

I pinched the get_avatar() function from the wp/wp-includes/pluggable.php file and commented out the setting of the avatar as an tag and returned the actual image url instead.


Copy this code and paste it in your HTML
  1. function get_avatar_url($id_or_email, $size = '96', $default = '', $alt = false) {
  2. if (! get_option('show_avatars')) { return false; }
  3.  
  4. if (false === $alt) { $safe_alt = ''; }
  5. else { $safe_alt = esc_attr($alt); }
  6.  
  7. if (!is_numeric($size)) { $size = '96'; }
  8.  
  9. $email = '';
  10. if (is_numeric($id_or_email)) {
  11. $id = (int) $id_or_email;
  12. $user = get_userdata($id);
  13. if ($user) { $email = $user->user_email; }
  14. }
  15.  
  16. elseif (is_object($id_or_email)) {
  17. // No avatar for pingbacks or trackbacks
  18. $allowed_comment_types = apply_filters('get_avatar_comment_types', array( 'comment'));
  19. if (!empty($id_or_email->comment_type) && ! in_array($id_or_email->comment_type, (array) $allowed_comment_types)) { return false; }
  20.  
  21. if (!empty($id_or_email->user_id)) {
  22. $id = (int) $id_or_email->user_id;
  23. $user = get_userdata($id);
  24. if ($user) { $email = $user->user_email; }
  25. }
  26.  
  27. elseif ( !empty($id_or_email->comment_author_email) ) { $email = $id_or_email->comment_author_email; }
  28. }
  29.  
  30. else { $email = $id_or_email; }
  31.  
  32. if (empty($default)) {
  33. $avatar_default = get_option('avatar_default');
  34. if (empty($avatar_default)) { $default = 'mystery'; }
  35. else { $default = $avatar_default; }
  36. }
  37.  
  38. if (!empty($email)) { $email_hash = md5(strtolower($email)); }
  39.  
  40. if (is_ssl()) { $host = 'https://secure.gravatar.com'; }
  41. else {
  42. if (!empty($email)) { $host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash{0} ) % 2 ) ); }
  43. else { $host = 'http://0.gravatar.com'; }
  44. }
  45.  
  46. if ('mystery' == $default) {
  47. $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('[email protected]')
  48. }
  49. elseif ('blank' == $default) { $default = includes_url('images/blank.gif'); }
  50. elseif (!empty($email) && 'gravatar_default' == $default) { $default = ''; }
  51. elseif ('gravatar_default' == $default) { $default = "$host/avatar/s={$size}"; }
  52. elseif (empty($email)) { $default = "$host/avatar/?d=$default&s={$size}"; }
  53. elseif (strpos($default, 'http://') === 0) { $default = add_query_arg('s', $size, $default); }
  54.  
  55. if (!empty($email)) {
  56. $out = "$host/avatar/";
  57. $out .= $email_hash;
  58. $out .= '?s='.$size;
  59. $out .= '&d=' . urlencode($default);
  60.  
  61. $rating = get_option('avatar_rating');
  62. if (!empty($rating)) { $out .= "&r={$rating}"; }
  63.  
  64. //$avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
  65. $avatar = $out;
  66. }
  67.  
  68. else {
  69. //$avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
  70. $avatar = $default;
  71. }
  72.  
  73. return $avatar;
  74. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.