Skip to:
Content
Pages
Categories
Search
Top
Bottom

Need help fixing code so all default wordpress avatars are used on buddypress


  • unbelievable
    Participant

    @unbelievable

    So i made this code which successfully fetches wordpress avatar to display on buddypress it does sync the profile pictures but it dooesn’t change all the profile pictures on buddypress for example the avatar where it says “Whats new” still has mystery man

    What am i missing to have it successfully sync on all places in buddypress including in friends section help please because parts are synced but not everything

    // Add the custom avatar to BuddyPress user profiles
    add_filter('bp_core_fetch_avatar', 'custom_user_avatar', 10, 2);
    
    function custom_user_avatar($avatar, $params) {
        // Get the user ID from the parameters
        $user_id = isset($params['item_id']) ? $params['item_id'] : 0;
    
        // Specify the maximum size for the avatar (adjust as needed)
        $max_size = 170;
    
        // Determine the context where the avatar is being fetched
        $context = isset($params['object']) ? $params['object'] : '';
    
        // Adjust avatar size based on the context
        switch ($context) {
            case 'activity': // For member activity avatars
                $avatar_size = 50; // Set the size you want for activity avatars
                break;
            case 'group': // For group avatars (if applicable)
                $avatar_size = 100; // Set the size you want for group avatars
                break;
            default:
                $avatar_size = $max_size; // Fallback to max size for other contexts
                break;
        }
    
        // Fetch the avatar HTML for the user with the specified size
        $avatar_html = get_avatar($user_id, $avatar_size);
    
        // Check if the avatar HTML is not empty
        if (!empty($avatar_html)) {
            // Extract the URL from the avatar HTML
            preg_match('/src="(.*?)"/', $avatar_html, $matches);
            if (isset($matches[1])) {
                // Return the custom avatar URL with specified width and height
                return '<img src="' . esc_url($matches[1]) . '" class="avatar" style="max-width: ' . $max_size . 'px; height: auto;" />';
            }
        } else {
            // If no uploaded avatar is found, fall back to the default WordPress avatar with specified size
            $avatar_url = get_avatar_url($user_id, ['size' => $avatar_size]);
            return '<img src="' . esc_url($avatar_url) . '" class="avatar" style="max-width: ' . $max_size . 'px; height: auto;" />';
        }
    
        // Return the original avatar if avatar HTML could not be processed
        return $avatar;
    }
  • You must be logged in to reply to this topic.
Skip to toolbar