I forgot the comma above, should be a comma after ‘full’ , like so …
return bp_core_fetch_avatar( array(
'type' => 'full',
'item_id' => $user->ID,
'width' => 80,
'height' => 80,
) );
}
Also re. Avatar size, first try adding ‘type = full’ into the array, should allow you to specify larger sizes, as below …
return bp_core_fetch_avatar( array(
'type' => 'full'
'item_id' => $user->ID,
'width' => 80,
'height' => 80,
) );
}
Re. avatar size, not sure, you can try putting a span around your shortcode, give it a class and with CSS try to force the avatar size.
Or you can add a class into the fetch_avatar array and try that way.
See: https://www.buddyboss.com/resources/reference/functions/bp_core_fetch_avatar/
Try this for your avatar display.
Your shortcode syntax would look like …
[showavatar username=”johnsmith”]
You can change width & height to your preference.
function show_avatar ($atts) {
$user = get_user_by( 'login' , $atts['username'] );
return bp_core_fetch_avatar( array(
'item_id' => $user->ID,
'width' => 50,
'height' => 50,
) );
}
add_shortcode( 'showavatar' , 'show_avatar' );
This is what I came up with and I think it worked to fix the Avatar quality, however, it won’t impact the Profile images for Groups and Group cover photos. Can you point me in the correct direction for the Constants that work with those two?
Here’s my code:
// Set the BP_AVATAR_ORIGINAL_MAX_WIDTH to a higher value
if ( ! defined( 'BP_AVATAR_ORIGINAL_MAX_WIDTH' ) ) {
define( 'BP_AVATAR_ORIGINAL_MAX_WIDTH', 1024 ); // Set to match your largest expected upload size
}
// Adjust the quality of the avatar resizing process
function custom_bp_avatar_quality( $args ) {
$args['quality'] = 100; // Set to the highest quality
return $args;
}
add_filter( 'bp_core_avatar_resize_args', 'custom_bp_avatar_quality' );
add_filter( 'bp_core_avatar_thumb_resize_args', 'custom_bp_avatar_quality' );
add_filter( 'bp_core_group_avatar_resize_args', 'custom_bp_avatar_quality' );
add_filter( 'bp_core_group_avatar_thumb_resize_args', 'custom_bp_avatar_quality' );
// Adjust JPEG quality for all images in WordPress
add_filter( 'jpeg_quality', function() {
return 100; // Set to the highest quality
});
// Ensure high-resolution images are used in the HTML output
function custom_bp_use_full_size_avatar( $html, $params ) {
if ( isset( $params['object'] ) && in_array( $params['object'], array( 'user', 'group' ) ) ) {
// Get the full-size avatar URL
$full_avatar_url = bp_core_fetch_avatar( array(
'item_id' => $params['item_id'],
'object' => $params['object'],
'type' => 'full',
'html' => false,
));
// Construct the new img tag with the full-size avatar URL
$html = sprintf(
'<img src="%s" class="%s" alt="%s" width="%d" height="%d"/>',
esc_url( $full_avatar_url ),
esc_attr( $params['class'] ),
esc_attr( $params['alt'] ),
(int) $params['width'],
(int) $params['height']
);
}
return $html;
}
add_filter( 'bp_core_fetch_avatar', 'custom_bp_use_full_size_avatar', 10, 2 );
// Add custom CSS to scale down the avatars
function custom_bp_add_avatar_css() {
$custom_css = "
.avatar,
.group-avatar img {
width: 150px;
height: 150px;
object-fit: cover; /* Ensures the image covers the element's entire area */
}
";
wp_add_inline_style( 'bp-parent-css', $custom_css ); // Adjust 'bp-parent-css' to match your theme's main stylesheet handle if necessary
}
add_action( 'wp_enqueue_scripts', 'custom_bp_add_avatar_css' );
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;
}
Salut ! Effectivement, BuddyPress ne supprime pas automatiquement les avatars des utilisateurs lorsque leurs comptes sont supprimés. Cependant, tu peux gérer ce problème en ajoutant un peu de code personnalisé à ton thème ou dans un plugin spécifique. Voici comment tu pourrais procéder pour supprimer les avatars lors de la suppression des comptes d’utilisateurs :
1- Crée un hook dans ton fichier functions.php ou dans un plugin spécifique :
Utilise l’action bp_remove_user ou wpmu_delete_user, delete_user selon la configuration de ton réseau si tu es en multisite ou pas.
2- Ajoute le code suivant pour supprimer l’avatar lorsqu’un utilisateur est supprimé :
function remove_user_avatar_on_delete($user_id) {
// Vérifie si l'utilisateur a un avatar
$avatar_path = bp_core_fetch_avatar(array(
'item_id' => $user_id,
'html' => false,
'type' => 'full',
'no_grav' => true
));
if ($avatar_path) {
// Supprime le fichier avatar
@unlink($avatar_path);
}
}
// Hook pour la suppression de l'utilisateur
add_action('delete_user', 'remove_user_avatar_on_delete');
add_action('wpmu_delete_user', 'remove_user_avatar_on_delete');
add_action('bp_remove_user', 'remove_user_avatar_on_delete');
Note : Ce code utilise @unlink pour supprimer le fichier, qui supprime silencieusement le fichier sans afficher d’erreur si le fichier n’existe pas ou ne peut être supprimé. Assure-toi que le chemin de l’avatar est correctement récupéré.
3- Teste le code : Avant de mettre ce code en production, teste-le dans un environnement de développement pour t’assurer qu’il fonctionne comme attendu sans effets secondaires.
Cela devrait aider à gérer le problème des fichiers d’avatar résiduels. Si tu as besoin de plus d’informations sur la fonction ou sur d’autres façons de gérer les fichiers avec WordPress et BuddyPress, n’hésite pas à demander !
Two ways to use your BP profile pic as your WordPress avatar:
1. Plugins:
Install and activate a plugin like “BuddyPress Default Avatar” or “BuddyPress Avatar Sync.”
These plugins automatically sync your BP profile photo with your WordPress avatar, making it a simple and hands-off solution.
2. Custom code:
If you prefer a more technical approach, you can add code to your theme’s functions.php file.
This code snippet will enable syncing of the two avatars:
PHP
function bp_avatar_sync($user_id) {
// Get BP profile picture URL
$bp_avatar_url = bp_core_fetch_avatar(array(‘item_id’ => $user_id, ‘type’ => ‘full’));
// Update WordPress avatar URL
update_user_meta($user_id, ‘user_avatar’, $bp_avatar_url);
}
add_action(‘bp_core_avatar_uploaded’, ‘bp_avatar_sync’);
Remember, both methods achieve the same result. Choose the method that best suits your technical skills and preferences.
To use your BP (BuddyPress) profile picture as the WordPress avatar, you can follow these steps:
Install and activate the “BuddyPress” plugin: Ensure that you have the BuddyPress plugin installed and activated on your WordPress site. This plugin adds social networking functionality, including user profiles, to your WordPress site.
Upload a profile picture in BuddyPress: Go to your BuddyPress profile and upload the desired profile picture. BuddyPress allows users to customize their profile information, including the profile picture. Typically, you can find the profile settings in the user dashboard or by navigating to your profile page on the front-end of your website.
Enable BuddyPress avatar syncing: By default, BuddyPress doesn’t automatically sync the profile picture with the WordPress avatar. You can enable this functionality by adding custom code to your theme’s functions.php file or by using a plugin specifically designed for this purpose.
Option 1: Custom code:
Open your theme’s functions.php file (Appearance > Theme Editor > functions.php) and add the following code snippet:
php
Copy code
function bp_sync_profile_picture_with_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
if ( function_exists( ‘bp_core_fetch_avatar’ ) ) {
$avatar = bp_core_fetch_avatar( array(
‘item_id’ => $id_or_email->user_id,
‘type’ => ‘full’,
‘html’ => false
) );
}
return $avatar;
}
add_filter( ‘get_avatar’, ‘bp_sync_profile_picture_with_avatar’, 10, 5 );
Option 2: Use a plugin:
Install and activate a plugin like “BuddyPress Default Avatar” or “BuddyPress Avatar Sync.” These plugins simplify the process by automatically syncing the BuddyPress profile picture with the WordPress avatar.
Verify the avatar sync: Log out of your WordPress account and visit your website as a guest or open an incognito/private browsing window. Leave a comment or view your author bio to see if the BuddyPress profile picture is now being used as the WordPress avatar.
By following these steps, your BuddyPress profile picture should now be synced with your WordPress avatar.
Hello,
I would like to change the default Buddypress avatar type.
In bp-core-avatars.php, “bp_core_fetch_avatar” function is set to “thumb”.
How to set to “Full” in my theme ?
Thanks for your help !
It is unclear if this is a BuddyPress issue.
BP does not store avatar info in the database.
It calls avatars from the uploads > avatars directory and organizes them based on the WP user ID.
See bp_core_fetch_avatar
If not found, the default image is used.
There are several filter hooks you could try.
Look towards the bottom of function bp_core_fetch_avatar
in this file:
wp-content\plugins\buddypress\bp-core\bp-core-avatars.php
For example, I would try this one first:
function gabriel_random_avatar( $default_grav, $params ) {
// $default_grav is an url
// replace it with the full url to one of your preferred avatars
return $default_grav;
}
add_filter('bp_core_avatar_default', 'gabriel_random_avatar', 99, 2 );
Are you saying that you want to use BP avatars on WP author pages?
If so, find the call to the avatar on the authors template.
And replace it with something like this:
<img class="" src="<?php
$authorUserID = get_the_author_meta('ID'); // get the user id of the post author
echo bp_core_fetch_avatar (
array( 'item_id' => $authorUserID, // output user id of post author
'type' => 'full',
'html' => FALSE // FALSE = return url, TRUE (default) = return url wrapped with html and css classes
)
);
hello
i writed this code part for wordpress :
<?php $av_id=bp_get_group_id();?>
<div class="img_group" style="background: no-repeat url('<?php echo(bp_core_fetch_avatar('item_id='.$av_id.'&object=group&no_grav=true&html=false'));?>');" ></div>
in my google chrome code inspector i have this html output :
<div class="img_group" style="background: no-repeat url("http://localhost/001-blueprint/wp-content/uploads/group-avatars/2/5f3252e75e000-bpthumb.png");"></div>
and i have this css line in my chrome inspector too, i have image miniature on flyhover so image seems to be charged.
element.style {
background: no-repeat url(http://localhost/001-blueprint/wp-content/uploads/group-avatars/2/5f3252e75e000-bpthumb.png);
}
problem, i don’t have anything on my website …
Hi guys, how are you?
I have a website that shows the correct avatar (from Social Login) in comments but in likes it gets the user uploaded profile, even if the social login is forcing the new one (it get’s it directly from the upload folder).
The wrong code is:
$avatar = bp_core_fetch_avatar(
array(
'html' => false,
'type' => 'thumb',
'item_id' => $user_id
)
);
// Get User Image Code.
$output .= "<a data-yztooltip='" . $fullname . "' style='background-image: url( " . $avatar . ");' href='" . bp_core_get_user_domain ( $user_id ) . "'></a>";
It returns:
/wp-content/uploads/avatars/38/5f1df11236f8e-bpthumb.jpg (the old photo before social login)
The code that shows the correct one:
<?php bp_activity_avatar( ‘type=thumb&user_id=’ . bp_get_activity_comment_user_id() ); ?>
It returns:
//graph.facebook.com/2434567832184997/picture?type=large
Tried changing but it shows the author profile pic on the liked space.
Can anyone help me?
Thanks!
You can use for logged in user:
bp_displayed_user_avatar( ‘type=full’ );
if calling within a group:
bp_group_member_avatar();
or in general (this is pulled for admin of a group, but can be set up in other ways):
bp_core_fetch_avatar( array( ‘item_id’ => $admin->user_id, ’email’ => $admin->user_email, ‘type’ => ‘full’ ) )
Hello,
I want to create an email-token to display a member avatar in a bp-custom-mail.
I tried to implement snippets from this tutorial, but maybe I don’t understand everything correctly.
1) I created a single-bp-email.php with custom html content which works fine.
There I can add the output whereever I want the avatar to be displayed <?php bp_email_avatar(); ?>, right?
2) To my bp-custom.php I added this function:
function bp_email_avatar() {
$token = '{{recipient.avatar}}';
printf( '<img src="%s">', $token );
}
Have a look at my bp-custom.php:
https://1drv.ms/u/s!AikSNhBAk4teugLf_yT-8610eUDC
$formatted_tokens['recipient.avatar'] = bp_core_fetch_avatar(
array(
'object' => 'user',
'item_id' => $formatted_tokens['initiator.id'],
'html' => false,
)
);
=> The last snippet results in a php-error.
Could you help me to make this work?
Thanks in advance,
Chris
Hi @mohamedbakry83,
In this case you’ll need to copy the email template into a /buddypress/assets/email/single-bp-email.php
file of your active theme and edit it from theme to include a new template tag.
something like:
<?php mohamedbakry83_output_avatar(); ?>
Then in a bp-custom.php file, you’ll need to include new function for this template tag:
function mohamedbakry83_output_avatar() {
$token = '{{recipient.avatar}}';
printf( '<img src="%s">', $token );
}
In this file, you’ll need to use & adapt the filter @shanebp pasted in his first reply making sure to check $formatted_tokens['friend-requests.url']
& $formatted_tokens['initiator.id']
are set and then set the recipient.avatar token like this :
$formatted_tokens['recipient.avatar'] = bp_core_fetch_avatar(
array(
'object' => 'user',
'item_id' => $formatted_tokens['initiator.id'],
'html' => false,
)
);
This is for the friendship request email. But you should be able to do it for any email type with this example.
Welp, I couldn’t wait so I just spent hours trying to figure this out – with SUCCESS. If anyone stumbles upon this, here is the code I placed in my theme’s function.php file.
function jb_get_bp_user_avatar($avatar, $id_or_email, $size, $default, $alt)
{
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);
}
if ($user && is_object($user)) {
$avatar = bp_core_fetch_avatar(array(‘item_id’ => $user->ID));
}
return $avatar;
}
add_filter(‘get_avatar’, ‘jb_get_bp_user_avatar’, 10000, 5);
Try add_filter(‘bp_core_fetch_avatar_no_grav’, ‘__return_true’);
There was a bug ticket that used this ages ago – https://buddypress.trac.wordpress.org/ticket/7056
Can’t guarantee it still works.
Hi all,
I really need help. I can not find the bug by myself. I am creating a custom widget to be able to display on the homepage of my site the groups a user is a member of (whether the group is hidden or not).
I managed to retrieve the groups and their avatar and then display them in the widget. Everything works well but as soon as I want to place it in front of others, there are display bugs. The other widgets fit in my widget!
Here is the code I wrote in my functions.php :
// Display groups a user is member of + linked
function user_group_memberships( $user_id = null, $show_hidden = false ) {
$group_ids = groups_get_user_groups($user_id);
$i = 1;
$visible_group_ids = array();
foreach($group_ids["groups"] as $group_id) {
if (!$show_hidden) {
if(groups_get_group(array( 'group_id' => $group_id )) -> status !== 'hidden') {
$visible_group_ids[] = $group_id;
}
} else {
$visible_group_ids[] = $group_id;
}
}
if (empty($visible_group_ids)) {
echo 'None';
} else {
echo '<ul id="" class="" aria-live="polite" aria-relevant="all" aria-atomic="true">';
foreach($visible_group_ids as $visible_group_id) {
$avatar = bp_core_fetch_avatar( array(
'item_id' => $visible_group_id,
'object' => 'group',
'type' => $type,
'avatar_dir' => 'group-avatars',
'alt' => $alt,
'css_id' => $id,
'class' => 'avatar',
'width' => $width,
'height' => $height
));
?>
<li class="odd public is-member group-has-avatar">
<div class="item-avatar">
<a href="' <?php echo home_url() . '/groups/' . groups_get_group(array( 'group_id' => $visible_group_id )) -> slug ?>'"><?php echo $avatar ?></a>
</div>
<div class="item">
<div class="item-title">
<a href="' <?php echo home_url() . '/groups/' . groups_get_group(array( 'group_id' => $visible_group_id )) -> slug ?>'"><?php echo groups_get_group(array( 'group_id' => $visible_group_id )) -> name ?></a><?php echo (end($visible_group_ids) == $visible_group_id ? '' : ' ' )?>
</div>
</div>
</li>
<?php if ($i++ == 3) break;
}
echo '</ul>';
}
}
And here the code I use in my Widget :
return user_group_memberships(get_current_user_id(), true);
So this is a very hacky way i’m doing it. I noticed using xprofile_set_field_data to set the data for an extended profile field only works temporarily. However, overwriting an existing default profile field (i.e. Facebook, Snapchat) seems to stay permanently.
xprofile_set_field_data('Snapchat', $current_user->ID, $avatar_path);
So as admin, I went in and changed the name of the default profile field to my desired one (i.e. Snapchat -> path) and applied
add_filter( 'xprofile_updated_profile', 'update_xprof' );
function update_xprof() {
$path=bp_core_fetch_avatar();
xprofile_set_field_data('path', $current_user->ID, $path);
}
This way works more or less. However, I noticed that the function only kicks in when the user literally clicks ‘Edit’ and changes the data in her/his profile from the front end.
This function does not kick in whenever the user changes his/her avatar. I tried to use
xprofile_avatar_uploaded
in place of xprofile_updated_profile
, but with it, the function doesn’t seem to work at all. Can someone comment on why?
I’ve decided to create a hidden xprofile field to store this data, but using xprofile_set_field_data only seems to work temporarily, after several minutes, the entire row containing the data disappears from wp_bp_xprofile_data. How can I make this permanent?
add_action( 'bp_setup_nav', 'write_path' );
function write_path() {
global $bp;
global $current_user;
$avatar_path = bp_core_fetch_avatar('html=false');
xprofile_set_field_data('path', $current_user->ID, $avatar_path);
}
Hi Debby, I found time to test this, if you install WP Featherlight and then make the following change:
<?php $fullsize_avatar_url = bp_core_fetch_avatar( array( 'type' => 'full', 'html' => false, 'no_grav' => true, 'item_id' => bp_get_member_user_id() ) ); ?>
<li <?php bp_member_class(); ?>>
<div class="item-avatar">
<a href="#" data-featherlight="<?php echo $fullsize_avatar_url; ?>"><?php bp_member_avatar(); ?></a>
</div>
You will have your avatars in lightboxes. However these will probably be small. The default is only 150 pixels.
You can change this with the following setting placed in either your child themes functions.php or a bp-custom.php placed in the plugins directory.
define ( 'BP_AVATAR_FULL_WIDTH', 150 );
define ( 'BP_AVATAR_FULL_HEIGHT', 150 );
change the 150 to your preferred pixel size for the image.
What you will need however is details of how to construct the url for the full size avatar image:
<?php $fullsize_avatar_url = bp_core_fetch_avatar( array( 'type' => 'full', 'html' => false, 'item_id' => bp_get_member_user_id() ) ); ?>
So you’d add this as the image that will load into the lightbox.