Search code examples
phpwordpressbuddypress

Set a jpg as avatar on Buddypress


I'm setting a front end ajax profile editing with Wordpress, Buddypress. I already upload the picture in the folder wp-content/uploads/avatar/Idoftheuser but after that I'm a little lost.

I've been looking for documentation but most of it just point me on how to do it via de wp-admin, I need to do it with php.

I found on buddypress how they handle their crop on bp-core-avatars.php as:

$r = array(
    'item_id'       => $avatar_data['item_id'],
    'object'        => $avatar_data['object'],
    'avatar_dir'    => $avatar_dir,
    'original_file' => $original_file,
    'crop_w'        => $avatar_data['crop_w'],
    'crop_h'        => $avatar_data['crop_h'],
    'crop_x'        => $avatar_data['crop_x'],
    'crop_y'        => $avatar_data['crop_y']
);

// Handle crop.
if ( bp_core_avatar_handle_crop( $r ) ) {

The thing is hat I dont get where they set the user who the crop is for, what if its a new unlogged user?

I'd like to not use bp_core_avatar_handle_crop but simply assign the jpg I'm already uploading as the profile avatar.


Solution

  • what if its a new unlogged user?

    How can you do front-end profile editing for a user who is not logged in? BP does not allow avatar uploads if a user is not logged in. I think there are plugins that allow avatar upload on registration, probably on a create user hook.

    If you know the user ID, you can upload to the avatar dir for that user. For a user with an ID of 8, the structure that BP expects is:

    wp-content/uploads/avatar/8/8-bpfull.jpg
    wp-content/uploads/avatar/8/8-bpthumb.jpg