Skip to main content
SnippetsWordpress

Avatar from Facebook Profile

Use a Facebook profile picture as the WordPress author avatar.

Here’s the WordPress filter and a function to replace the avatar of an author with the id of 2. Change the ID to that of the author you want to assign a Facebook profile pic.

add_filter( 'get_avatar' , 'my_custom_avatar' , 1 , 5 );
function my_custom_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
    $user = false;
    if ( is_numeric( $id_or_email ) ) {
        $id = (int) $id_or_email;
        $user = get_user_by( 'id' , $id );
    } elseif ( is_object( $id_or_email ) ) {
        if ( ! empty( $id_or_email->user_id ) ) {
            $id = (int) $id_or_email->user_id;
            $user = get_user_by( 'id' , $id );
        }
    } else {
        $user = get_user_by( 'email', $id_or_email );	
    }
    if ( $user && is_object( $user ) ) {
        if ( $user->data->ID == '2' ) {
            $avatar = 'http://graph.facebook.com/_FB_USER_ID_/picture?redirect=1&height=200&width=200&type=normal';
            $avatar = "<img alt='{$alt}' src='{$avatar}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
        }
    }
    return $avatar;
}

Put together the Facebook profile picture URL. This appears to be depreciated, but it still works… Visit https://findmyfbid.in/ to grab the FB USER ID. Simply place this ID in the function above 🙂

https://codex.wordpress.org/Plugin_API/Filter_Reference/get_avatar

https://developers.facebook.com/docs/graph-api/reference/v7.0/user/picture

Contact us to learn more about Adopt the Web for your business

Author Jarod Thornton

More posts by Jarod Thornton