So I finally figured the solution out with the help of DeepSeek AI;
first, I created a user meta with ACF plugin named: acf_user_avatar.
then I mapped the upload file field of my form to this new meta key by Gravity Forms User Registration Addone.
and then DeepSeek provided me a code that would set the received photo as the user’s avatar:
//solution for setting user avatar from gravity forms's upload file field
add_action('gform_user_registered', 'save_user_avatar_from_media_library', 10, 4);
function save_user_avatar_from_media_library($user_id, $feed, $entry, $password) {
// Get the uploaded file URL from the form entry
$file_url = rgar($entry, '31'); // Replace '31' with your file upload field ID
if (empty($file_url)) {
return; // Exit if no file was uploaded
}
// Get the attachment ID from the file URL
$attachment_id = attachment_url_to_postid($file_url);
if (!$attachment_id) {
return; // Exit if the attachment ID cannot be found
}
// Save the attachment ID to user meta
update_user_meta($user_id, 'custom_user_avatar', $attachment_id);
}
function custom_get_avatar($avatar, $id_or_email, $size, $default, $alt) {
$user = false;
// Get the user object
if (is_numeric($id_or_email)) {
$user = get_user_by('id', $id_or_email);
} elseif (is_object($id_or_email)) {
if (!empty($id_or_email->user_id)) {
$user = get_user_by('id', $id_or_email->user_id);
}
} else {
$user = get_user_by('email', $id_or_email);
}
// Check if the user has a custom avatar
if ($user && $user->data->ID) {
$attachment_id = get_user_meta($user->data->ID, 'custom_user_avatar', true);
if ($attachment_id) {
// Get the image URL from the attachment ID
$avatar_url = wp_get_attachment_image_url($attachment_id, [ $size, $size ]);
if ($avatar_url) {
return '<img src="' . esc_url($avatar_url) . '" width="' . esc_attr($size) . '" height="' . esc_attr($size) . '" alt="' . esc_attr($alt) . '" class="avatar avatar-' . esc_attr($size) . '" />';
}
}
}
// Fallback to the default avatar if no custom avatar is found
return $avatar;
}
add_filter('get_avatar', 'custom_get_avatar', 10, 5);
I should also note that I had to install Gravity Perks addone so that I could enable save to media library option in the settings of the upload file field of the form.