BuddyDev

Search

[Resolved] Custom avatar for gender

  • Participant
    Level: Initiated
    Posts: 1
    Slaven Beram on #24956

    Hi, I’m using this code to load gender specific avatars but it doesnt work, can you help?

    I added this to my functions.php file of the theme, however it always goes to the Unknown avatar. It doent matter if I chhhese male or female

    <?php

    function onecommunity_js_functions_child() {

    wp_enqueue_script( ‘onecommunity-js-functions-child’, get_stylesheet_directory_uri() . ‘/js/functions.js’, true );

    }

    add_action( ‘wp_enqueue_scripts’, ‘onecommunity_js_functions_child’ );

    function dont_save_join_group_activity( $activity_object ) {

    $exclude = array( ‘joined_group’ );

    if( in_array( $activity_object->type, $exclude ) )
    $activity_object->type = false;

    }
    add_action(‘bp_activity_before_save’, ‘dont_save_join_group_activity’, 1, 1 );

    add_filter( ‘bp_core_fetch_avatar_no_grav’, ‘__return_true’ );

    function buddydev_set_default_avatar_based_on_profile_field_data( $avatar, $params ) {

    $user_id = $params[‘item_id’];
    $field_id_or_name = “field_19”;//Gender field Id, You may use field name e.g ‘Gender’;
    //I suggest using id for performance reasons

    $gender = xprofile_get_field_data( $field_id_or_name, $user_id );
    $gender = strtolower( $gender );
    if ( $gender == ‘male’ ) {
    $avatar = ‘http://www.beyondacode.com/wp-content/uploads/2019/07/male.png’;
    } elseif( $gender ==’female’ ) {
    $avatar = ‘http://www.beyondacode.com/wp-content/uploads/2019/07/female.png’;
    } else {
    $avatar = ‘http://www.beyondacode.com/wp-content/uploads/2019/07/unknown.png’;
    }

    //note, you may play with the $param[‘type’] and use different image for full|thumb
    return $avatar;
    }
    add_filter( ‘bp_core_default_avatar_user’, ‘buddydev_set_default_avatar_based_on_profile_field_data’, 10, 2 );

    ?>

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

    Hi Slaven,
    Thank you for opening the topic.
    Please do the following

    1. Change

    
    $field_id_or_name = "field_19";
    
    

    to

    
    
    $field_id_or_name = 19;
    

    I am assuming that 19 is your field id.

    Does that make it work?

    If not, please visit Dashboard->Users->Profile Field and edit the field.
    Please make sure to disable “Autolinking” form the right side and save. Now, check again and let me know if it works or not?

    Regards
    Brajesh

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

    Closing this ticket due to lack of reply.

    Also, for others looking at this topic, you may use BuddyPress Member Types pro to set custom default avatars & covers for genders(genders can be created as member type).

    https://buddydev.com/plugins/buddypress-member-types-pro/

The topic ‘ [Resolved] Custom avatar for gender’ is closed to new replies.

This topic is: resolved