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?

有帮助吗?

解决方案 2

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.

其他提示

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/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top