简体   繁体   中英

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.

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

You can do this while registration, because you need user id to set avatar. After user is created using create user hook, you can upload images manually to following format, say for userid (22),

wp_content/uploads/avatars/22/22-bpfull.jpg (150x150px) 
wp_content/uploads/avatars/22/22-bpthumb.jpg (50x50px)

or

wp_content/uploads/avatars/22/anyname-bpfull.jpg 
wp_content/uploads/avatars/22/anyname-bpthumb.jpg

Following is sample code,

function addMyCustomData($user_id,$meta) {
  //your stuff
}
add_action('user_register','addMyCustomData',10,2,);

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM