5

So here's the deal..I have Wordpress + bbPress integrated with a membership software (aMember).

On my bbPress forums, under people's username i want to show the Wordpress roles (Not bbpress roles) of every member and also an image depending of the role of every member.

For example,

If user role is subscriber -> Show role under username in bbpress -> Also show an image below.

The reason why i want to show Wordpress roles (instead of bbpress roles) is that my membership software (amember) allows me to set different wordpress roles depending on the User's subscription. I have 2 different membership plans on my site (one free and on paid) and i want to show different images in my bbpress forums based on their plan.

I went through the bbPress templates and i found this code (in loop-single-reply.php):

<?php bbp_reply_author_link( array( 'sep' => '<br />', 'show_role' => true ) ); ?> // this shows the bbpress role
<?php echo 'Points: '.cp_getPoints(bbp_get_reply_author_id()); ?> // this shows the member points below the username - I use a points plugin)

Now how can i replace this code with a code that shows the Wordpress roles (not bbpress) for every user and also show an image under it depending on what roles it is. For example:

If Is Role "Subscriber" -> Show Role + Image Under It

If Is Role "Contributor" -> Show Role + Image Under It

If Is Role "Administrator" -> Show Role + Image Under It

I'm not a programmer so i have no idea how to accomplish this. Please help. I found some related code that i think i could use to make this work:

<?php if ( current_user_can('contributor') ) : ?>
Content
<?php endif; ?>

Now my failed attempt looks like this:

<?php 

$user_roles = $current_user->roles;
$current_user = $bbp_get_reply_author_id; // i think this is wrong :P
$user_role = array_shift($user_roles);
?>
<?php if ($user_role == 'administrator') : ?>

Show Role  
Show Image

<?php elseif ($user_role == 'editor') : ?>

Show Role
Show Editor Image

<?php elseif ($user_role == 'author') : ?>

Show Role
Show Author Image

<?php elseif ($user_role == 'contributor') : ?>

Show Role
Show Contributor Image

<?php elseif ($user_role == 'subscriber') : ?>

Show Role
Show Subscriber Image

<?php else : ?> 

Show Role

<?php endif ?>

I have no idea what i'm doing...The code above is something i found on Google.

Can anyone help?

I would really appreciate it.

Allen Payne
  • 105
  • 1
  • 2
  • 9

3 Answers3

14

It would look something like this to test if a user is a subscriber.

<?php
global $current_user; 
get_currentuserinfo(); 
if ( user_can( $current_user, "subscriber" ) ){ 
// Show Role
// Show Subscriber Image
} 

in your case if you want to do multiple user checks i would use a switch statement like so

global $current_user; 
get_currentuserinfo();
switch (true)  {
 case ( user_can( $current_user, "subscriber") ):
   // Show Role
   // Show Subscriber Image
 break;
 case ( user_can( $current_user, "contributor") ):
   // Show Role
   // Show Contributor Image
 break;
 case ( user_can( $current_user, "administrator") ):
   // Show Role
   // Show Administrator Image
 break;
}

you can continue the switch statement with more user roles.

EDITED

global $current_user; 
get_currentuserinfo();
switch (true)  {
 case ( user_can( $current_user, "subscriber") ):
   echo '<img src="http:www.impho.com/images/001.jpg">';
 break;
 case ( user_can( $current_user, "contributor") ):
   echo '<img src="http:www.impho.com/images/002.jpg">';
 break;
 case ( user_can( $current_user, "administrator") ):
  echo '<img src="http:www.impho.com/images/003.jpg">';
 break;
}

EDITED BASED ON USER REQUESTS

Ok this should do it, replace all the code that you have getting user name,avatar, points and image using the following code.

In your functions place this

function userLooping($role, $img)
{
$user_query = new WP_User_Query( array( 'role' => $role ) );

// User Loop
if ( !empty( $user_query->results ) ):
    foreach ( $user_query->results as $user ):
        echo '<p>'. $user->display_name.'</p>'; // display user name
        echo '<p>'.get_avatar($user->ID).'</p>'; // display user avatar
        //echo '<p>Points: '.cp_getPoints(bbp_get_reply_author_id()).'</p>';
        echo '<p>'.$img.'</p>'; //display image based on role
    endforeach;
endif;
}

remove the // in front of echo above

Place the following inside your template

<?php 
$args = array( array('role' => 'administrator', 'img' => '<img src="http://placeape.com/100/100">'),array('role' => 'subscriber', 'img' => '<img src="http://placekitten.com/100/100">'));
foreach ($args as $arg):
 userLooping($arg['role'],$arg['img']);
endforeach;
?>

to add more roles and images, just add a new array after subscriber

David Chase
  • 2,055
  • 1
  • 18
  • 25
  • thank you for your answer. it tried your code and it gives me this error: Parse error: syntax error, unexpected ')', expecting ':' or '{' in. The file where i'm trying to use this code is a bbpress template file, if that makes any difference. Just thought i should let you know. – Allen Payne Mar 11 '13 at 02:25
  • forgot to mention that i need to use bbp_get_reply_author_id() to show the images for each member in bbpress forums. so i don't think get_currentuserinfo() would work. not sure, please correct me if i'm wrong. – Allen Payne Mar 11 '13 at 02:28
  • sorry about that try it again i had an extra parenthesis in my switch statement i edited the code and posted again – David Chase Mar 11 '13 at 02:44
  • Still not working...I get this error: Parse error: syntax error, unexpected T_BREAK... – Allen Payne Mar 11 '13 at 03:07
  • While you're at it, can you also tell me how can i replace get_currentuserinfo(); with bbp_get_reply_author_id(); to apply the code to my bbpress forums? (see my reply above) – Allen Payne Mar 11 '13 at 03:08
  • i tested the code above and works fine, may i see where you are trying to implement such as a page or template and how you are doing it. and i believe `bbp_get_reply_author_id` is not what you need for the each members image.. – David Chase Mar 11 '13 at 03:14
  • I managed to get the code not to show any errors but now i have another problem. I placed it in the bbpress template but it doesn't show anything under username. (it's like it doesn't work at all). You can see an example bbpress post here: http://www.impho.com/forums/topic/the-last-member-to-post-here-is-the-best-on-impho – Allen Payne Mar 11 '13 at 03:22
  • One the bbpress post, where you see the Points...They are shown there using this code: ...This is why i think bbp_get_reply_author_id() is the key here. – Allen Payne Mar 11 '13 at 03:28
  • Here's a screenshot with the code in my template, maybe it helps: http://img515.imageshack.us/img515/5069/imagesqkj.jpg – Allen Payne Mar 11 '13 at 03:34
  • are you just trying to get a users avatar? how does the user change their image?? and based on your image it does work because you dont have any additional code instead of my comments `// Show Role` is not code – David Chase Mar 11 '13 at 03:44
  • the avatar has nothing to do with this. I just want to show an image (something like this http://www.jooria.com/files/thumb/2743caee5d.gif) below each username (below avatar) based on the role of the user. so as you see in the code that shows the points, bbp_get_reply_author_id() gets the username of each forum poster. (i think :P) – Allen Payne Mar 11 '13 at 03:53
  • are those users registered on your wordpress, i mean under users do they exist? if so then you can use my way and not bbp_get_reply_author_id – David Chase Mar 11 '13 at 03:58
  • Yes all the users are registered on wordpress. I tried again with your code and i keep getting this error: Parse error: syntax error, unexpected '<'... Is this how it's supposed to look like? http://img818.imageshack.us/img818/3599/image2mfx.jpg – Allen Payne Mar 11 '13 at 04:11
  • Another thing...You said to use get_currentuserinfo(); ... but doesn't that mean that it shows the image only to the user that is logged in? I want it to show the images to all users in the forum, at all times, just show different images below their username based on their wordpress role. Basically, in the forum, below where it shows the Points: XXXX i want to show the image based on that user's wp role. – Allen Payne Mar 11 '13 at 04:14
  • take a look at the edited answer i will adjusted so it works at all time :) – David Chase Mar 11 '13 at 04:20
  • Well thanks for the 'echo' part, but it still doesn't work. It doesn't show anything below the username. http://www.impho.com/forums/topic/the-last-member-to-post-here-is-the-best-on-impho/ – Allen Payne Mar 11 '13 at 04:27
  • No matter what i do, it still doesn't show anything. I even tried to change the code to use the If/Else chain instead of switch(true). It shows nothing on my page, it's like it doesn't work at all. – Allen Payne Mar 11 '13 at 04:58
  • Ok, so i managed to partially get it to work. Now it shows the same image to all users, even to admin (Allen is an admin account). See here: http://www.impho.com/forums/topic/how-to-make-10k-per-month/ – Allen Payne Mar 11 '13 at 05:12
  • Alright, so your code is now working. But it still doesn't do what i want. Now when i log in with an admin account it shows one image to all forum members, when i log in with a subscriber account it shows a different image to all forum members and so on. What i want is to show images to all users even when logged out, and show a different one depending on their role. ( not depending of the current_user - the one that is logged in). How can i do that? Everything else is working, except this. OMG it's already 7.30 AM and i haven't slept all night. This damn code :) – Allen Payne Mar 11 '13 at 05:24
  • David i found a much simpler solution. I'll add the code below. Thank you for all your help. – Allen Payne Mar 11 '13 at 16:29
  • @AllenPayne thats what i gave you from the start with the switch case, you just add some of your own code with `bbp_get_reply_author_id()` can you mark it as the accepted answer since you used my code but modified it?? – David Chase Mar 11 '13 at 16:34
  • Of course David. I'm still new to stackoverflow. Not sure how it all works yet :) Should i delete the answer below? – Allen Payne Mar 11 '13 at 16:37
  • @AllenPayne thanks so much! glad it all worked out for you :) – David Chase Mar 11 '13 at 17:06
  • This seems to apply for everyone with that "role" and higher? So using 'subscriber' means Admins, Authors, etc. see that stuff too. – Nathan Mar 31 '20 at 19:15
1

I FOUND A MUCH SIMPLER SOLUTION:

<?php
global $forumuser; 
$forumuser = bbp_get_reply_author_id();
switch (true)  {
case ( user_can( $forumuser, "author") ):
echo '<img src="http://www.impho.com/images/image1.png">';
break;
case ( user_can( $forumuser, "premium") ):
echo '<img src="http://www.impho.com/images/image2.png">';
break;
case ( user_can( $forumuser, "starter") ):
echo '<img src="http://www.impho.com/images/image3.png">';
break;
case ( user_can( $forumuser, "subscriber") ):
echo '<img src="http://www.impho.com/images/image4.png">';
break;
}
?>

The starter and premium roles are custom roles which i created using the User Role Editor plugin.

Everything seems to be working fine now.

Allen Payne
  • 105
  • 1
  • 2
  • 9
1

I know this is a bit late but here is my solution which i believe is the shortest and most efficient:

$current_user = new WP_User(wp_get_current_user()->id);
$user_roles = $current_user->roles; 
foreach ($user_roles as $role) {

   if  ($role == 'subscriber' ){
     //code here for subscribers
   }

   if  ($role == 'editor' ){
     //code here for editors
   }

}
danyo
  • 5,686
  • 20
  • 59
  • 119