简体   繁体   中英

BuddyPress / Wordpress Get dynamic user ID

I'm trying to make a function that overrides the current users avatar URL with a custom URL that is saved in their user meta. So far the function works except that I'm stumped trying to figure out how to have the function grab the user ID of the user buddypress/wordpress is getting an avatar for.

The code so far looks like this:

// return new URL for avatar
function wpse_49216_my_new_avatar_url() {

// get user_id of user bp/wp is getting avatar for
$user_id = bp_get_member_user_id(); 

// get avatar name from above user's meta
$avatar_choice = get_user_meta($user_id, "avatar_choice", true);

if($avatar_choice) {
  return 'path_to_avatars/'.$avatar_choice.'.png';
} else {
  return 'path_to_avatars/default-avatar.png';
}
}

add_filter( 'bp_core_fetch_avatar_url', 'wpse_49216_my_new_avatar_url' );

// Replace image src="" with the new avatar URL
function wpse_49216_filter_bp_avatar( $html ) {
   return preg_replace( '/src=".+?"/', 'src="' . wpse_49216_my_new_avatar_url() . '"', $html      );
}
add_filter( 'bp_core_fetch_avatar', 'wpse_49216_filter_bp_avatar' );

The main problem is if I set the $user_id to the current logged in user's ID, then all avatars will be load the avatar of the current logged in user. And bp_get_member_user_id() only works on member pages. I need something that works universally. Does any Wordpress/Buddypress expert have any idea how I can get the correct user ID?

I need something that works universally

If you look at the function you are filtering, you'll see that it depends on knowing 'where' you are in BP. Since it is driven by context, there is no universal approach.

您可以在functiona下面使用Global用户ID:

bp_displayed_user_id()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM