1

I am new and a bit of troublesome on how Wordpress work as I have plan to use it as CMS for my commercial website. I have been figuring out on how to make an image that I have retrieved from user's Facebook photo and save it as upload file in Wordpress.

Let me explain first.

I have created a Facebook test user and able to get their Facebook information after their registration (by getting some basic info and use wp_insert_user()). I have no problem retrieving basic user information as I just save it in my Wordpress site with update_user_meta. I also able to get user's Facebook photos image link with Facebook API.

But right now I need those image to be save as Wordpress media so that users can manipulate them (update or delete). I tried to make like; during user registration, my website able to get the users photos file and save it in my site. When user have logged in, they will able to view their Facebook photos in my site and they able to delete the photos and upload a new one if they wanted to.

Let say for example, this is the retrieved user profile photo:-

http://graph.facebook.com/118114855253013/picture?type=large 

and user's Facebook cover photo:-

https://scontent.xx.fbcdn.net/hphotos-xft1/v/t1.0-9/12931266_119567738441058_7589195259803266908_n.jpg?oh=d709e39e7525c69e96a2b1695d4930fa&oe=57C011F2

from Facebook API:-

$response = $fb->get('/me?fields=id, name, cover, picture, albums{name,photos{source}}',  $longLivedAccessToken);
$user = $response->getGraphUser();

$coverpic = $user['cover']['source'];
$profilepic = 'http://graph.facebook.com/' . $user['id'] . '/picture?type=large';

Usually I will directly save it to user meta along with other basic information for example:-

update_user_meta( $user_id, 'coverpic', $coverpic );
update_user_meta( $user_id, 'profilepic', $profilepic );

But that just link the images from Facebook. I may be need to change that update_user_meta() method and change it into I don't know, maybe Wordpress upload function such as wp_handle_upload() or wp_insert_attachment() or maybe other related functions that is quite confusing for me. But I have been reading Wordpress documentation for days and having hard time on applying them in my current situation. I have been figuring out on how to save it in Wordpress media and make it able to be manipulated by the users themselves in their front end profile page after their 'Wordpress Login via Facebook' in my site.

I do really need help here guys. My programming brain is so slow, I might kill myself to become good at it...

2

You can use media_sideload_image() which will take a link to a file and upload it to the media library. You do need to pass it some kind of $post_id @Sumit points out in the comments you could pass NULL into the $post_id field.

media_sideload_image(
    $file_url,
    $post_id,
    $image_desc,
    $return
);

If you need the uploaded image ID, take a look Tom J. Nowells answer using hooks

  • 1
    I just checked it $post_id can be NULL as well :) – Sumit Apr 15 '16 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.