Make the Custom Profile Image as the Default in WordPress/Gravatar

Uncategorized

If you’re creating a custom registration form with custom fields including the Profile Image, copy and paste the custom function below to your child theme’s functions.php to set the custom profile image field as the default display picture for user profiles.

// Replace Gravatar with custom profile picture
function custom_gravatar($avatar, $id_or_email, $size, $default, $alt) {
    $user = false;

    if (is_numeric($id_or_email)) {
        $id = (int) $id_or_email;
        $user = get_user_by('id', $id);
    } elseif (is_object($id_or_email)) {
        if (!empty($id_or_email->user_id)) {
            $id = (int) $id_or_email->user_id;
            $user = get_user_by('id', $id);
        }
    } else {
        $user = get_user_by('email', $id_or_email);
    }

    //REPLACE _wc_memberships_profile_field_profile_picture WITH THE NAME OF YOUR CUSTOM FIELD
    if ($user && $custom_picture = get_user_meta($user->ID, '_wc_memberships_profile_field_profile_picture', true)) {
		$custom_picture_url = wp_get_attachment_image_src($custom_picture, 'large');
        $avatar = "<img alt='{$alt}' src='{$custom_picture_url[0]}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
    }

    return $avatar;
}
add_filter('get_avatar', 'custom_gravatar', 10, 5);

Leave a Reply

Your email address will not be published. Required fields are marked *