Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress -> If Is Role Subscriber Show Image?

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.

like image 792
Allen Payne Avatar asked Dec 06 '22 08:12

Allen Payne


1 Answers

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

like image 198
David Chase Avatar answered Dec 12 '22 04:12

David Chase