Gender based user avatar

I'm creating a matrimonial website. I want to set the user avatar based on the gender(I have a custom_user_meta for the user's gender) But I don't understand how to get the "user id" in order to use the meta data "user_meta_gender"(to find out if the user is male or female).

P.S: I'm not using buddypress, I'm using a plugin named Profile Builder Pro Also get_current_user_id does not work since it retrieves only the LOGGED IN users ID.

``This is the code I used in my functions.php:

function dynamic_user_gravatar($current_dp){

$id = get_the_ID();  //This is where I want to figure out a way to find the user id, this won't work.
$key = "user_gender";
$single = true;

$gender = get_user_meta($id, $key, $single);
if($gender == 'Male'){
    $myavatar = 'http://localhost:81/matrimony/wp-content/uploads/2020/04/groom.png';
}else{
    $myavatar = 'http://localhost:81/matrimony/wp-content/uploads/2020/04/bride.png';
 }
 return $myavatar;
}

add_filter( 'get_avatar', 'dynamic_user_gravatar');

Additionally I figured out that I can return the relevant users avatar by the parameter "$current_dp", in case if there is no direct method to get the user_id can we user $current_dp to retrieve the user_id from the avatar? ``

Topic gravatar user-meta avatar profiles customization Wordpress

Category Web


I managed to come up with the answer :D Thanks to @Jos

function ht1_change_avatar($args, $id_or_email) {
    $gender = get_user_meta($id_or_email, 'user_gender', true);
    if($gender=='Male'){
        $myavatar = 'http://localhost:81/matrimony/wp-content/uploads/2020/04/groom.png';
    }else{
        $myavatar = 'http://localhost:81/matrimony/wp-content/uploads/2020/04/bride.png';
    }
    $args['url'] = $myavatar;
    return $args;
}
add_filter('get_avatar_data', 'ht1_change_avatar', 100, 2);

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.