For a WordPress/BuddyPress website, if you want to allow your user to upload their photo as avatar after registration on a custom page, during or after the registration process, it is not that straight forward.
To make life easier, you can use Simple Local Avatar plugin to upload a custom avatar to your local directory. Install and activate this plugin.
For the photo upload page, just check the value of the avatar field from the user_meta table and if it is blank, you can show the page, otherwise proceed to the next step or page.
This is how you check the value of existing avatar:
$pic = get_user_meta($id, 'simple_local_avatar', true);
if($pic['full']) echo $pic['full'];
else { echo 'go to upload page'; }
On your custom upload page, add following code. It will create the simple_local_avatars object and add the upload field code in the form.
global $user_ID;
if ($user_ID) {
$user_info = get_userdata($user_ID);
$id = $user_info->ID;
}
if (is_user_logged_in()) {
if(isset($_POST['user_avatar_edit_submit'])){
do_action('edit_user_profile_update', $id);
}
echo '
‘; }else { echo ‘user not logged in!’; }
Now check your page and it should show you either the image path or the form to upload avatar. BTW, if you are using the BuddyPress this avatar will not appear in your BP profile pages. For that, add following functions in your themes functions.php file:
add_filter('bp_core_fetch_avatar', 'local_bp_insert_avatar', 3, 5);
function local_bp_insert_avatar($avatar = '', $params, $id) {
if(!is_numeric($id) || strpos($avatar, 'gravatar') === false) return $avatar;
$pic = get_user_meta($id, 'simple_local_avatar', true);
if (!$pic['full'] || $pic['full'] == '') return $avatar;
$avatar = preg_replace('/src=("|\').*?("|\')/i', 'src=\'' . $pic['full'] . '\'', $avatar);
return $avatar;
}
The above code will nicely show the local avatar for the BP profiles as well.
Hope that helps!
Thanx Ali for your code, it really help me.
But I only use the last
add_filter(‘bp_core_fetch_avatar’, ‘local_bp_insert_avatar’, 3, 5);
And the problem is it takes the full size avatar image, I tru to replace “full” with “thumbnail” but then it breakes, any idea if is possible to use an specific thumbnail size ?