0

I want to replace the default "Mystery Man" profile picture with a different picture.

As you know, you can access someone's Facebook profile picture by going to http://graph.facebook.com//picture (replace with the UID of the requested account.

So what I basically did was adding my theme's function file these few lines:

 add_filter( 'avatar_defaults', 'newgravatar' );  


function newgravatar ($avatar_defaults) {  
    $myavatar = 'http://graph.facebook.com/'.get_current_user_id().'/picture';
    $avatar_defaults[$myavatar] = "FB Profile Picture";  
    return $avatar_defaults;  
}

The problem is that wordpress doesn't show this URL directly. It stores the picture on WordPress.com's servers. As a result, the picture is always the same picture and doesn't change when a different user logs in.

Is there any way to prevent WordPress from caching the picture on their servers? Or is there any other way to do what I want to do?

Ido
  • 397
  • 2
  • 7
  • 22

2 Answers2

0

If someone has a gmail account with a custom avatar and that email address is used to register it will auto use that avatar. I know this might not be exactly what you're looking for but I thought it was pretty useful to know.

UPDATE:

Found your answer here:

http://buildinternet.com/2009/02/how-to-change-the-default-gravatar-in-wordpress/

Phillip Berger
  • 2,317
  • 1
  • 11
  • 30
  • Actually, what they're saying is exactly what I did, and it didn't work... It's almost the same code. The problem is that Wordpress caches the pictures, which causes the problem. If there was a way to prevent Wordpress from caching the pictures it would work. – Ido Mar 02 '13 at 21:12
  • I think after you make the changes in 24 hours Wordpress will refresh the file and cache it for another 24. – Phillip Berger Mar 02 '13 at 21:26
  • That doesn't help... Each user has a different picture. Even if wordpress will refresh it in 24 hours, it won't solve the problem. The picture has to be rendered all the time so each user will get a different picture. It's hard to explain what I'm talking about. I hope you understand what the problem is so you can help me solve it. – Ido Mar 02 '13 at 21:55
  • Look, every facebook user has a different ID. In order that every user will get his own facebook picture I used this URL: http://graph.facebook.com//picture The moment WordPress stores the image on their servers, the picture won't change for each user. It will be permanent. User X will see Picture X, but also user Y and user Z will see picture X. That is the problem. – Ido Mar 02 '13 at 22:01
  • Ah ok. Your oringal question asked about a default instead of mystery man so I didn't realize you meant per user, not default. – Phillip Berger Mar 02 '13 at 22:03
0

I finally managed to fix it by using this filter:

    add_filter('get_avatar', 'new_fb_insert_avatar', 1, 5);



function new_fb_insert_avatar($avatar = '', $id_or_email, $size = 96, $default = '', $alt = false) {



  $id = 0;

  if (is_numeric($id_or_email)) {

    $id = $id_or_email;

  } else if (is_string($id_or_email)) {

    $u = get_user_by('email', $id_or_email);

    $id = $u->id;

  } else if (is_object($id_or_email)) {

    $id = $id_or_email->user_id;

  }

  if ($id == 0) return $avatar;

  $pic = get_user_meta($id, 'fb_profile_picture', true);

  if (!$pic || $pic == '') return $avatar;

  $avatar = preg_replace('/src=("|\').*?("|\')/i', 'src=\'' . $pic . '\'', $avatar);

  return $avatar;

}

the get_user_meta($id, 'fb_profile_picture', true); brings the user's custom avatar picture. Every user has a different picture, and it is stored in his user-meta information.

Ido
  • 397
  • 2
  • 7
  • 22