Advertisement:

Author Topic: [TUTORIAL] How to retrieve user facebook profile picture  (Read 4717 times)

Tex70

  • Newbie
  • *
  • Posts: 10
[TUTORIAL] How to retrieve user facebook profile picture
« on: April 27, 2014, 03:52:19 am »
This tutorial is for people who using the plugin Facebook Connect and wishes to show the user profile's image near the user's name.
Step 1
Open your current theme and edit the file header.php, put this code before the line with...<!DOCTYPE html PUBLIC... etc:

<?php
$config = array (
'appId' => 'your app ID',
'secret' => 'your secret code',
'allowSignedRequest' => false // Optional but should be set to false for non-canvas apps
);

$facebook = new Facebook($config);
$user_id = $facebook->getUser();

if ($user_id) {

// We gave a user ID, so probably a logged in user.
// if not, we'll get an exception which we handle below

try {

$user_profile = $facebook->api('/me','Get');
} catch (FacebookApiException $e){

// If the user is logged out, you can have an user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll just ask the user to login again here.

$login_url = $facebook->getLoginUrl();
error_log ($e->getType());
error_log ($e->getMessage());

}

}else{

//No User, print a link for the user to login
$login_url = $facebook->getLoginUrl();

}
?>

Step 2
Now if the user is a logged facebook user, we show the associated picture
Always in the header.php, before this code that show the name, <span><?php echo sprintf(__('Hi %s',...etc...
Put this code:

<?php if ($user_id == 0) {
echo "";
}else{
echo "<img src=\"https://graph.facebook.com/$user_id/picture\" height=\"32\" width=\"32\" />&nbsp;";
} ?>

If you want can change the values in height and width as you wish.

Note
This is not my work, i've seen a video tutorial and to make it easy for everybody i decided to write the code showed, now you can simply copy and paste!
« Last Edit: April 27, 2014, 04:00:15 am by Tex70 »

thedaigle

  • Jr. Member
  • **
  • Posts: 81
Re: [TUTORIAL] How to retrieve user facebook profile picture
« Reply #1 on: October 23, 2014, 12:01:19 am »
Not able to get this working quite right. Can I see a link to your site?

thedaigle

  • Jr. Member
  • **
  • Posts: 81
Re: [TUTORIAL] How to retrieve user facebook profile picture
« Reply #2 on: October 23, 2014, 12:40:52 am »
Nevermind- I got it.

Both code sets go in 'header.php' as mentioned. Through me off whenever you said above the doctype, etc.

Thanks!

Zacdaniel

  • Full Member
  • ***
  • Posts: 115
  • Why, Yes I'm a white african haha...
Re: [TUTORIAL] How to retrieve user facebook profile picture
« Reply #3 on: November 12, 2014, 01:26:22 pm »
I can't seem to get it to work on my side

Zacdaniel

  • Full Member
  • ***
  • Posts: 115
  • Why, Yes I'm a white african haha...

Zacdaniel

  • Full Member
  • ***
  • Posts: 115
  • Why, Yes I'm a white african haha...
Re: [TUTORIAL] How to retrieve user facebook profile picture
« Reply #5 on: November 13, 2014, 12:26:31 am »
got it working

wiggie

  • Newbie
  • *
  • Posts: 1
Re: [TUTORIAL] How to retrieve user facebook profile picture
« Reply #6 on: April 08, 2015, 07:27:16 am »
Hello Guys

I have question here ,

how can i display the facebook Picture on Public profile ?

Thank you
Wiggie