Page 1 of 4

Displaying User Role on the Site

PostPosted: December 11th, 2011, 4:40 pm
by peeld
Hi there. I have the following code as a plugin on my site:

Code: Select all
function pc_get_userrole ($user_id) {
   $user = new WP_User($user_id);
   $userclean = $user->roles[0];
   return $userclean;
}


Then, where I want the user's role to display, I put this:

Code: Select all
echo pc_get_userrole(bp_current_user_id());


Works like a charm - on the site, the user's role gets displayed like this:
Code: Select all
s2member_level1


I know nothing about code; is there a way I can make it so that when the value returned is s2member_level1, as above, the text returned is something like "Auditing Student", and if the value returned is s2member_level1 the text is "Working Student"?

Where to put these statements?
This I know is a pretty simple/dumb question, but I'm a PHP dum dum...

thanks,
Daisy

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 5:24 pm
by Eduan
You can use this shortcode:
[s2Get constant="S2MEMBER_CURRENT_USER_ACCESS_LABEL" /]

Or you can use this PHP:
Code: Select all
<?php echo S2MEMBER_CURRENT_USER_ACCESS_LABEL; ?>

Hope this helps. :)

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 5:48 pm
by s2pidkaspr
i want to know how to get user roles displayed on comment section for viewers/visitors to see regardless if they were logged in or logged out.

For example:
COMMENT 1:
username: Mike
member status: Premium Member (s2member level 2 <<-- example)

COMMENT 2:
username: John
member status: Standard Member (s2member level 1 <<-- example)

Which "Premium" and "Standard" status are seen on comments. Even if the viewer/visitor is logged in or logged out.

is this possible? if so, how? I tried the code

<?php echo get_user_field('s2member_access_label', $user_id); ?>

But it shows current logged in member's level status. And it shows on all comments. I want it like, the comment author's member level status to be seen.

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 5:52 pm
by Eduan
I believe I answered that question in my post above, so you might wanna take a look at it.

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 6:01 pm
by peeld
Actually, s2pidkaspr's questions is more what I meant to ask - implementation of that short code is ALMOST what I want - but it displays MY user status, not the user status of the member I'm looking at.

This is the closest I've gotten, your post sure has me excited!!! :)

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 6:04 pm
by s2pidkaspr
I actually tried that code you posted above. But it seems not working just the way I wanted it to be. So do you have any alternatives? Do you have Skype? please let's chat about it, i really need help on it. Thanks

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 6:09 pm
by peeld
I've been digging in the buddypress site for ages trying to find a solution to this, at least I'm getting closer!

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 6:12 pm
by Eduan
@s2pidkaspr
Could you post a screenshot of how you want it? So I can have a clear idea of how you want it.

Thanks.

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 6:21 pm
by s2pidkaspr
@Eduan
I have PM'd you the screenshot. Please check it and tell me if you know how to do this.

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 6:37 pm
by peeld
Image

Here you go.

Daisy

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 7:00 pm
by s2pidkaspr
@Eduan
you still there? have you seen my private message?

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 7:03 pm
by Eduan
Thanks for the photos guys.
You might need to implement some PHP with HTML manually or hire someone to do it for you at: viewforum.php?f=37

@s2pidkaspr
Maybe something like:
Code: Select all
<font style="float: right;">[s2Get constant="S2MEMBER_CURRENT_USER_ACCESS_LABEL" /]</font> 


@peeld
You could try something like:
Code: Select all
<font style="float: left;">[s2Get constant="S2MEMBER_CURRENT_USER_ACCESS_LABEL" /]</font> 


@Both
Now I'm not sure this will accomplish what you want, but I think it's a start. Mainly what I did is make it so that it aligns to the side you want. I tried using the PHP in a test post and it didn't work for me, so I gave you the code with the shortcode.

Hope this helps. :)

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 7:16 pm
by s2pidkaspr
can i try that code using php? what is it?

well, all i wanted to do is display the comment authors their member status, so that visitors can see what are their status are.

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 7:28 pm
by Bruce C
You can put that into a post or page to view it.

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 7:35 pm
by peeld
That code just displays YOUR user label, not the label of the user you're viewing. :(

I'm too nervous to post for a job for hire :) Don't know who to trust :)

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 7:37 pm
by Bruce C
One sec, I'll find the function to pass the ID into.

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 7:48 pm
by peeld
Super, and if it's kosher PM me your Paypal email for a donation :)

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 8:15 pm
by Bruce C
You can put this into your s2hacks.php file (If you don't have it create it in wp-content/mu-plugins/s2hacks.php)

Code: Select all

<?php
function custom_get_user_role($id 1)
{
$user_role get_user_meta($id'wp_capabilities');
//You should customize the array values below to what you would like to show for each.
foreach ($user_role[0] as $key=>$value){
    if(
$value == 1){
        echo 
str_replace(array('access_s2member_','ccap_','level0''level1','basic','administrator'),array('','','Free Subscriber''Paid User','Basic User''Administrator'),$key);}
    }}
?>


Now, you can do this in a post with Exec-PHP installed.

Code: Select all
//This gives you the user role for the user with an ID of 110
<?php echo custom_get_user_role(110); ?>

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 8:21 pm
by s2pidkaspr
@Bruce C
where can i find mu-plugins?

is it possible for me to use this too?

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 8:27 pm
by Bruce C
You need to create the file/directory if you haven't already.

Yes, it's possible. BUT, you're going to need to make some custom code to get it to work correctly with your setup, unless you're already passing the user id into your system, in which case you can just pass that into the function. The function merely takes the ID and gets the role for the user.

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 8:29 pm
by Bruce C
@peeld

Are you running on BuddyPress? Because I believe there is a function to get the ID of the user that page belongs to.

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 8:38 pm
by peeld
Yes, I'm running buddypress. I was wondering where the ID was going to come from... :) I really know NOTHING about PHP, so if you know the code that would be helpful :) I know just enough to get in trouble :)

Daisy

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 8:41 pm
by Bruce C
I would need to take a look at your setup, I only know what I can from reading the source code, my local server crashes every time I try to install BuddyPress.

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 8:44 pm
by peeld
Well, gettin' closer. The code you provided breaks the backend of the site when it's put in my s2hacks file.

Daisy

Re: Displaying User Role on the Site

PostPosted: December 11th, 2011, 8:47 pm
by Bruce C
Missing bracket. Sorry, it didn't show up in the post I had it in.

Try the edited code