Skip to:
Content
Pages
Categories
Search
Top
Bottom

Changing Avatar based on field data


  • dutch84
    Participant

    @dutch84

    On my website there are six categories of users. It is helpful to members to be able to easily identify which category each member fits into. So, to help them, I am trying to create a color-coded system for each user group. For example:

    Butcher -> Red
    Baker -> Blue
    Candlestick Maker -> Yellow

    I saw somewhere that you can create a bp-custom.php file. Then, add some css rules in your styles.css file. I’m not sure what I am doing wrong, but it did not work.

    Firstly, I created a bp-custom.php file and stuck it in my buddypress folder. Contents of the file are as follows:

    function buddydev_create_css_class_based_on_profile_field_data( $class,  $object, $params ) {
     
        $field_id = 25; //Professional Title field Id
     
        $professionaltitle = xprofile_get_field_data(  $field_id );
        $professionaltitle = strtolower( $professionaltitle);
        
        foreach ($professionaltitle->title as $title) {
            $class = $title.'avatar';
        }
     
        
        return $class;
    }
    add_filter( 'bp_core_avatar_class', 'buddydev_create_css_class_based_on_profile_field_data', 10, 3 );

    Then I created css rules for each professional title in the style.css for my child-theme like

    .butcheravatar {
       border: 3px solid red;
    }

    Does anyone have any idea where I may have gone wrong?

    Thanks in advance!

Viewing 7 replies - 1 through 7 (of 7 total)

  • Prashant Singh
    Participant

    @prashantvatsh

    Hi, please check the following code:

    function buddydev_create_css_class_based_on_profile_field_data( $class,  $object, $params ) {
     
        $field_id = 25; //Professional Title field Id
     
        $professionaltitle = xprofile_get_field_data(  $field_id );
        $professionaltitle = strtolower( $professionaltitle);
        $class = $professionaltitle.'avatar';
        
        return $class;
    }
    add_filter( 'bp_core_avatar_class', 'buddydev_create_css_class_based_on_profile_field_data', 10, 3 );

    Thanks


    dutch84
    Participant

    @dutch84

    Thank you @prashantvatsh
    It still is not working. Could these files possibly be in the wrong folders?


    Prashant Singh
    Participant

    @prashantvatsh

    Yes, it seems so. bp-custom.php is a file that resides in your WordPress ‘plugins’ folder, I think you have created that in BuddyPress folder. Please put that in the plugins folder.


    dutch84
    Participant

    @dutch84

    Thanks again, @prashantvatsh
    It worked!
    However, it only shows on the profile of the user. I want the color to show in the activity feed and everywhere you see their avatar on the website. Is that possible? How do I achieve this?

    Thanks!


    dutch84
    Participant

    @dutch84

    @prashantvatsh it seems that I need to give activity avatar’s class based on field data so that they will take the css change as well. How and where do I update the php so that the Activity avatars receive a new class based on field data?


    dutch84
    Participant

    @dutch84

    @prashantvatsh Sorry to bother you, but I tried the following solution, and it is still not working:

    First, I added the following code to my bp-custom.php file:

    <?php
    
    //The following creates avatar class in activity stream
    //based on x-profile field data
    
    function buddydev_create_avatar_class_based_on_field_data() {
        
    	$user_id = bp_get_activity_user_id();
    	$title = xprofile_get_field_data('25', $user_id); // field ID or name
    	$title = strtolower( $title );	
    	$class = $title.'avatar';
    	
    	echo $class;
    }
    
    add_action( 'create_activity_avatar_class_based_on_field_data', 'buddydev_create_avatar_class_based_on_field_data', 10, 1 );
    
    ?>

    Then, I added the following as a class inside the div tag for activity avatar in my buddypress/bp-templates/bp-legacy/buddypress/activity/entry.php file:

    <?php do_action( 'create_activity_avatar_class_based_on_field_data'); ?>

    Yet still it is not working. Did I put the do_action in the wrong file?


    Prashant Singh
    Participant

    @prashantvatsh

    Hi,
    There is a filter code already written to modify this:

    return apply_filters( 'bp_get_activity_avatar', bp_core_fetch_avatar( array(
    			'item_id' => $item_id,
    			'object'  => $object,
    			'type'    => $type,
    			'alt'     => $alt,
    			'class'   => $class,
    			'width'   => $width,
    			'height'  => $height,
    			'email'   => $email
    		) ) );

    You can use this filter to write your own code to modify the class parameter.

    Thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.
Skip to toolbar