Created
September 16, 2025 11:26
-
-
Save vapvarun/17ae6d5a73db89e16e1837cf30fddf44 to your computer and use it in GitHub Desktop.
BuddyX Pro Theme - Fix for header avatar not updating with BuddyPress profile picture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * BuddyX Pro Theme - Fix Header Avatar Not Updating | |
| * | |
| * Problem: The avatar in the top-right navigation bar doesn't update when users | |
| * change their BuddyPress profile picture. | |
| * | |
| * Solution: This code snippet forces WordPress to use BuddyPress avatars when | |
| * an email address is used to fetch avatars, ensuring the header displays | |
| * the correct BuddyPress profile picture. | |
| * | |
| * How to use: | |
| * 1. Add this code to your child theme's functions.php file, OR | |
| * 2. Create a custom plugin with this code, OR | |
| * 3. Add it using a code snippets plugin | |
| * | |
| * @package BuddyX-Pro-Avatar-Fix | |
| * @version 1.0.0 | |
| * @author Varun Dubey <vapvarun@gmail.com> | |
| */ | |
| // Prevent direct access | |
| if ( ! defined( 'ABSPATH' ) ) { | |
| exit; | |
| } | |
| /** | |
| * Fix BuddyPress avatar not showing in header navigation | |
| * | |
| * This filter intercepts get_avatar() calls and ensures BuddyPress avatars | |
| * are used when the function is called with an email address. | |
| * | |
| * @param string $avatar The avatar HTML image tag | |
| * @param mixed $id_or_email User ID, email address, or user object | |
| * @param int $size Avatar size in pixels | |
| * @param string $default Default avatar URL | |
| * @param string $alt Alternative text for avatar | |
| * @return string Modified avatar HTML | |
| */ | |
| add_filter( 'get_avatar', 'buddyx_fix_header_avatar_display', 10, 5 ); | |
| function buddyx_fix_header_avatar_display( $avatar, $id_or_email, $size, $default, $alt ) { | |
| // Only apply this fix for logged-in users | |
| if ( ! is_user_logged_in() ) { | |
| return $avatar; | |
| } | |
| // Check if BuddyPress is active and avatar function exists | |
| if ( ! function_exists( 'bp_core_fetch_avatar' ) ) { | |
| return $avatar; | |
| } | |
| // If an email address is passed, fetch BuddyPress avatar using user ID | |
| if ( is_string( $id_or_email ) && is_email( $id_or_email ) ) { | |
| $user = get_user_by( 'email', $id_or_email ); | |
| if ( $user && $user->ID ) { | |
| // Fetch the BuddyPress avatar for this user | |
| $bp_avatar = bp_core_fetch_avatar( array( | |
| 'item_id' => $user->ID, | |
| 'type' => 'thumb', | |
| 'width' => $size, | |
| 'height' => $size, | |
| 'class' => 'avatar avatar-' . $size, | |
| 'html' => true, | |
| 'alt' => $alt, | |
| ) ); | |
| // Return BuddyPress avatar if successfully fetched | |
| if ( ! empty( $bp_avatar ) ) { | |
| return $bp_avatar; | |
| } | |
| } | |
| } | |
| // Return original avatar if no modification needed | |
| return $avatar; | |
| } | |
| /** | |
| * Alternative permanent fix (for theme developers) | |
| * | |
| * In the theme file: wp-content/themes/buddyx-pro/template-parts/header/buddypress-profile.php | |
| * Around line 587, change: | |
| * | |
| * FROM: echo get_avatar( $loggedin_user->user_email, 100 ); | |
| * TO: echo get_avatar( get_current_user_id(), 100 ); | |
| * | |
| * This ensures the avatar filter receives a user ID instead of email, | |
| * allowing BuddyPress to properly intercept and display its avatar. | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment