Hi, I created users profiles and control members via Toolset on my site. Along with this I'm using BuddyPress. Sync of names works but Image of profile photo is not. I have custom field created by Toolset for user photo. Could you please help to sync it with BuddyPress profile image?
Appreciate your help.
Thank you in advance!
Anton
Hello Anton and thank you for contacting the Toolset support.
I don't know how BuddyPress stores the profile image but I have found a document about its filters and I could find some filters by searching for the term "avatar" hidden link
For example the bp_core_fetch_avatar_url may be used to generate the avatar URL from your custom field. You can use the types_render_usermeta with the argument output='raw' to get the image URL.
https://toolset.com/documentation/customizing-sites-using-php/functions/#image
I hope this helps. Let me know if you have any questions.
sorry, I use toolset as not much experienced with custom code. Do not understand how to solve the issue with information above.
Custom code is beyond the scope of the support forum. If you are not comfortable with coding, you may need to hire a developer or one of our partners. https://toolset.com/contractors/
However, because BuddyPress is a popular plugin we may give it a try if you reproduce the issue on this test site hidden link
Thank you. Reproduced:
My profile page with controlled by toolset photo:
hidden link
Profile with buddypress photo:
hidden link
I run a quick test and the bp_core_fetch_avatar_url hook did not help. Maybe that's not the one used in the members' page that you prepared. I'll give it another try later today.
In the meantime, you may want to create a thread in the BuddyPress support forum, that would help us too.
I am not familiar with BuddyPress, so it took me some time to understand that the Members page use different hooks. Finally I was able to make it work with the following code:
add_filter( 'bp_member_avatar', 'my_bp_member_avatar', 999, 1);
function my_bp_member_avatar ( $html ) {
global $members_template;
$avatar = types_render_usermeta( "vase_foto", array(
"width" => 150,
"height" => 150,
"user_id" => $members_template->member->id,
"url" => true,
) );
if ( !empty( $avatar) ) {
return preg_replace( '/src=".+?"/', 'src="' . $avatar . '"', $html );
}
return $html;
}
You can check it here hidden link
The part of the code that is related to Toolset is this one:
global $members_template;
$avatar = types_render_usermeta( "vase_foto", array(
"width" => 150,
"height" => 150,
"user_id" => $members_template->member->id,
"url" => true,
) );
It calculates the thumbnail(150*150) of the image from the user's($members_template->member->id) custom field.
The other parts are related to how BuddyPress works.
Please note that this is custom code, and therefore out of the scope of the support forum. If you need to customize the user's avatar in other parts of the website(probably that uses another hook than bp_member_avatar), I have to kindly ask you to reach for assistance in the BuddyPress forum, in a general forum(StackExchage) or to hire a developer. You can find a list of our partners here https://toolset.com/contractors/
Thank you a lot for your help! Appreciate it!
Thank you for advising service for developers, I'll definitely use it.
Meantime with this task could you please help me a bit to finalize it. I found filter for update images on other pages :
hidden link
hidden link
But there an issue with
"user_id" => $members_template->member->id,
to define users correctly :
CODE:
add_filter( 'bp_core_fetch_avatar', 'my_bp_member2_avatar');
function my_bp_member2_avatar ( $html ) {
global $members_template2;
// $authorUserID = get_the_author_meta('ID');
$avatar2 = types_render_usermeta( "vase_foto", array(
"width" => 150,
"height" => 150,
'no_grav' => true,
"user_id" => $members_template->member->id,
"url" => true,
) );
if ( !empty( $avatar2) ) {
return preg_replace( '/src=".+?"/', 'src="' . $avatar2 . '"', $html );
}
// return $html;
}
To check that filter works I used code below:
$user_id = get_current_user_id();
avatar was changed in all places but of course on my picture for all users. the question is how to define respective users.
Appreciate if you will be able to help me.
Thank you
Anton
I put the code into functions.php
"user_id" => bp_displayed_user_id(),
it works for personal user page
hidden link
The last questions related group page:
hidden link
Hello and my apologies for the late reply, but I do not work on Wednesdays and Thursdays.
I believe that the group avatar uses a different hook than bp_core_fetch_avatar. For the members avatar, I used a function to log all the hooks, then I experienced with different hooks until I make it work.
add_action( 'all', 'log_hook_calls' );
function log_hook_calls() {
// Restrict logging to requests from your IP - your IP address goes here
$hook = current_filter();
if ( strpos( $hook, 'avatar' ) ) {
if ( in_array( $hook, ["get_avatar_url", "bp_core_avatar_url", "bp_core_fetch_avatar_no_grav", "bp_gravatar_url", "bp_core_fetch_avatar_url", "get_avatar", "bp_core_fetch_avatar", "bp_get_member_avatar", "bp_member_avatar"] ) )
error_log( $hook );
}
}
Use it, then visit the group page and check the debug.log file to see what hooks were used. Then, search within the BuddyPress base code to see how each hook is used(what arguments it expects).
Or, create a thread in the BuddyPress forum to ask for assistance. Keep in mind how to use types_render_usermeta to generate the image from the custom field.