BuddyDev

Search

Question about custom code to modify default avatars

  • Participant
    Nicolas Gonçalves on #11324

    Hi guys !
    Thank you for buddypress and its plugins, it helps me a lot lately 😀
    I just read the article about custom avatars based on type : https://buddydev.com/buddypress/using-different-default-avatar-buddypress-users-based-profile-fields-member-type/

    I’m trying to do kinda the same but based on a custom field i have to every profile. Can this code work anyway or do I have to find another option ?
    Here is my non working code, just in case !

    function buddydev_set_default_use_avatar_based_on_member_type( $avatar, $params) {
    $user_id = $params[‘item_id’];
    $type = get_user_meta( $user_id, ‘billing_user_type’, true );

    if ( $type == “DAD” ) {
    $avatar = get_bloginfo(‘url’). “/wp-content/themes/listable-child/mums/visuals/avatars/picto-cigogne-05.png”;// get_template_directory_uri() . ‘/assets/images/male.png’;
    }
    elseif( $type == “GRAN’DAD” ) {
    $avatar = get_bloginfo(‘url’). “/wp-content/themes/listable-child/mums/visuals/avatars/picto-cigogne-03.png”;//get_template_directory_uri() . ‘/assets/images/female.png’;
    }
    elseif( $type == “MUM”) {
    $avatar = get_bloginfo(‘url’). “/wp-content/themes/listable-child/mums/visuals/avatars/picto-cigogne-01.png”;
    }
    else {
    $avatar = ‘http://example.com/unknown.png’;//get_template_directory_uri() . ‘/assets/images/unknown.png’;;
    }

    return $avatar;
    }

    add_filter( ‘bp_core_default_avatar_user’, ‘buddydev_set_default_use_avatar_based_on_member_type’, 10, 2 );

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #11330

    Hi Nicolas,
    Will you be using BuddyPress custom field? If yes, you should consider using

    xprofile_get_field_data( $field_id_or_name, $user_id ); instead of the get_user_meta()

    If it is using the use meta(and not custom field).

    Also, If I were you, I will use get_template_directory_uri() instead of the get_bloginfo.

    The code should work though.

    You should inspect the user avatar src and see if the image src is correct.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved