Created
May 22, 2026 12:38
-
-
Save andrewlimaza/c414c1960bfa866880d3b02facb1b619 to your computer and use it in GitHub Desktop.
Sync Custom Avatars with BuddyPress
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 | |
| /** | |
| * Sync WordPress avatar into BuddyPress, with reliable size enforcement. | |
| * | |
| * @param string $avatar <img> tag or raw URL BP would return. | |
| * @param array $params Params passed to bp_core_fetch_avatar(). | |
| * @return string | |
| */ | |
| function my_pmpro_bp_sync_wordpress_avatar( $avatar, $params ) { | |
| if ( empty( $params['object'] ) || 'user' !== $params['object'] ) { | |
| return $avatar; | |
| } | |
| $user_id = ! empty( $params['item_id'] ) ? (int) $params['item_id'] : 0; | |
| if ( ! $user_id ) { | |
| return $avatar; | |
| } | |
| $width = my_pmpro_bp_resolve_avatar_size( $params['width'] ?? false, $params['type'] ?? 'thumb', 'width' ); | |
| $height = my_pmpro_bp_resolve_avatar_size( $params['height'] ?? false, $params['type'] ?? 'thumb', 'height' ); | |
| $wp_avatar_url = get_avatar_url( $user_id, [ | |
| 'size' => $width, | |
| 'default' => 'mystery', | |
| 'force_default' => false, | |
| ] ); | |
| if ( ! $wp_avatar_url ) { | |
| return $avatar; | |
| } | |
| // Always return a raw URL when html=false. | |
| if ( empty( $params['html'] ) ) { | |
| return esc_url( $wp_avatar_url ); | |
| } | |
| $alt = ! empty( $params['alt'] ) ? esc_attr( $params['alt'] ) : ''; | |
| $class = ! empty( $params['class'] ) ? esc_attr( $params['class'] ) : 'avatar'; | |
| $title = ! empty( $params['title'] ) ? sprintf( ' title="%s"', esc_attr( $params['title'] ) ) : ''; | |
| $id = ! empty( $params['css_id'] ) ? sprintf( ' id="%s"', esc_attr( $params['css_id'] ) ) : ''; | |
| // Inline style wins over any theme/BP stylesheet that does width:100%. | |
| return sprintf( | |
| '<img src="%s" width="%d" height="%d" style="width:%dpx;height:%dpx;max-width:%dpx;" alt="%s" class="%s"%s%s />', | |
| esc_url( $wp_avatar_url ), | |
| $width, | |
| $height, | |
| $width, | |
| $height, | |
| $width, | |
| $alt, | |
| $class, | |
| $title, | |
| $id | |
| ); | |
| } | |
| add_filter( 'bp_core_fetch_avatar', 'my_pmpro_bp_sync_wordpress_avatar', 20, 2 ); | |
| /** | |
| * Resolve an avatar dimension: explicit param → BP type function → hardcoded default. | |
| * | |
| * @param int|false $explicit Value from $params (may be false). | |
| * @param string $type 'thumb' or 'full'. | |
| * @param string $dimension 'width' or 'height'. | |
| * @return int | |
| */ | |
| function my_pmpro_bp_resolve_avatar_size( $explicit, $type, $dimension ) { | |
| if ( $explicit ) { | |
| return (int) $explicit; | |
| } | |
| $fn = sprintf( 'bp_core_avatar_%s_%s', $type === 'full' ? 'full' : 'thumb', $dimension ); | |
| if ( function_exists( $fn ) ) { | |
| return (int) $fn(); | |
| } | |
| return ( 'full' === $type ) ? 150 : 50; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment