Change S2Pro role label display

I have code placed where when a user changes their level, it displays in BuddyPress activity stream. But when this is posted to BuddyPress activity it shows as: USER has become a s2member_level2

I am trying to get the “s2member_level2” part (the role) to display as the label (have force labels set in S2Member settings). That didn’t work, so I also tried to change the role name in User Role Editor to no avail.

How do I get the role name to display as the modified name label?

Thanks.

Here is the code I am using to display role changes:

function buddydev_record_role_change_activity( $user_id, $role, $old_roles  ) {

$role = get_role( $role );
if ( ! $role ) {
	return ;
}

$action = sprintf( '%s has become a %s', bp_core_get_userlink( $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 );

As the notes in s2Member explain, forcing the labels works only on the admin pages – nowhere else.

You need to use the appropriate constant. Read through the page headed API/Scripting to see how to do what you are looking for.

Here is how to get “beauty names” of levels as coma-separated string, from a plugin of mine, you should adapt it to your needs:

$beautyroles = '';
for($n = 0; $n <= $GLOBALS["WS_PLUGIN__"]["s2member"]["c"]["levels"]; $n++) {
	$beautyroles .= (($beautyroles)?", ":'').format_to_edit($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["level".$n."_label"]);
}

So I have re-read the scripting area, and have found this code that would seem to display the Levels.

<?php echo S2MEMBER_CURRENT_USER_ACCESS_LABEL; ?>

However, when placed in the existing code,

`$action = sprintf( '%s has become a <?php echo S2MEMBER_CURRENT_USER_ACCESS_LABEL; ?>', bp_core_get_userlink( $user_id ), $role->name );`

it fails to display anything. Can anyone assist with why this placement does not work?

Thanks.

You can’t put PHP tags around something when you’re already in PHP.

So this:

<?php echo S2MEMBER_CURRENT_USER_ACCESS_LABEL; ?>'

should be something more like this:

' . echo S2MEMBER_CURRENT_USER_ACCESS_LABEL . '

Whether it will then do what you want for BuddyPress, though, I have no idea because I don’t use that plugin.

While I still have not resolved this, I have determined that using ‘current_user_access_level’ is likely to display the level of the current user (go figure). :slight_smile: not of the user name I need.

What I need to output there is the role (access_level) of the user named in the code ( bp_core_get_userlink( $user_id ) ).

I am trying to pull $role via get_role( $role) eg: $role = get_role( $role );

Does S2Member use ‘get_role’? or is there another term to use to get the role (access_level)? So that when I code:

bp_core_get_userlink( $user_id ), $role->name );

then $role->name will be the name of the access_level.

Thanks, and Happy New Year.

I think you misunderstand what get_role does. It doesn’t fetch the name of the role, but its functionality.

I think you should be using something like get_userdata( $user_id ) See https://codex.wordpress.org/Function_Reference/get_userdata