BuddyDev

Search

Display user role change in Activity stream

  • Participant
    Level: Initiated
    Posts: 2
    Mitch on #6816

    I was trying to figure a way to display a change in user role as an update to the BuddyPress Activity stream.

    For example, when a user first registers on our site, they are set as a ‘subscriber.’ I have removed new_member from displaying in the activity stream; however, I would like to display when the ‘subscriber’ is upgraded to ‘member’ (which we approve manually).

    Display something like:

    “Username became a member” (or ‘Username was upgraded to member’)
    2 hours ago

    Is this possible with one of your plugins? or with some code to bp-custom?

    Thanks and have a great day.

  • Keymaster
    (BuddyDev Team)
    Posts: 24593
    Brajesh Singh on #6821

    Hi Mitch,
    Please put this code in your bp-custom.php

    
    
    function buddydev_record_role_change_activity( $user_id, $role, $old_roles  ) {
    
    	$role = get_role( $role );
    	if ( ! $role ) {
    		return ;
    	}
    	//Brajesh has become Author
    	$action = sprintf( '%s has become %s', bp_core_get_user_displayname( $user_id ), $role->name );
    
    	xprofile_record_activity( array(
    		'user_id'   => $user_id,
    		'action'    => $action,
    		'content'   => '',
    		'type'      => 'role_changed'
    	) );
    
    }
    add_action( 'set_user_role', 'buddydev_record_role_change_activity', 10, 3 );
    
    

    Hope that helps.

  • Participant
    Level: Initiated
    Posts: 2
    Mitch on #6871

    Hello Brajesh,

    This is great. It worked first try, thanks so much.

    There were a couple of things I wanted to see if I could add. In the above code, it displays the user name, but it does not link like a normal activity post. I searched around and tried a few solutions (trial and error) 🙂 until finally this worked (so I thought):

    I changed:

    $action = sprintf( '%s has become %s', bp_core_get_user_displayname( $user_id ), $role->name );

    to

    $action = sprintf( '<a href="'. bp_get_loggedin_user_link() .'">%s</a> has become %s', bp_core_get_user_displayname( $user_id ), $role->name );
    

    I thought I was smart, only to find that link to MY profile, not to the user listed in the activity update. Whoops. 🙂

    If I wanted to make this user name display link to the user’s profile like a traditional activity stream update, how would I change this code?

    Thanks, and have a great day!

  • Keymaster
    (BuddyDev Team)
    Posts: 24593
    Brajesh Singh on #6872

    Hi Mitch,
    You may use this one

    
    
    $action = sprintf( '%s has become %s', bp_core_get_userlink( $user_id ), $role->name );
    
    

    Hope that helps.

  • Participant
    Level: Initiated
    Posts: 2
    Mitch on #6888

    Excellent Brajesh, thank you. I was close. In my guessing, I had also tried bp_core_get_user_link 🙂

    But what I can’t figure out is why ‘ $role->name ‘ displays the wrong WP Role. This code displays the default WP/S2Member role name, even though I have specifically changed the S2/WP role name (via User Role Editor).

    I also tried ‘ role->display_name ‘ and ‘ $user_info->role ‘ – neither worked (at least I tried) 🙂 and I just don’t know enough for where to look to resolve. Any ideas?

    Thanks again. Have a great day.

  • Keymaster
    (BuddyDev Team)
    Posts: 24593
    Brajesh Singh on #6890

    Hi Mitch,
    Yes, you were almost correct 🙂

    About the role name, can you please ask S2Members team if their roles are available via get_role? I am not a user of s2members, It will be better if they can provide some information.

    Thank you
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved