0

For all users to see. Not only the admin in cause. Also I am using a plugin called simple user ranking for displaying the user rank which is calculated for the number of comments posted, so I need to override this somehow; I dont want my admins to have a status of rookie or smth like that if he doesn't post a lot of comments. For displaying the rank Im using this in my comment-template.php

 <?php if( function_exists('get_user_rank') ) echo get_user_rank(); ?>

I want to something like:

  if ( current_user_can('...')) echo 'admin' else  ( current_user_can('....'))
  else if or end if : ( function_exists('get_user_rank') ) echo get_user_rank(); 

I have multiples choices when I add a new username like: moderator, editor, author ... maybe have a function based on these roles..

Thanks a bunch.

Note: asking because Im noob in creating a function like that.

Core of the plugin :

   if( !function_exists('get_user_rank') ) {

function get_user_rank() 
{   
    if( !$ranks = get_option('_user_rank_comments_fields') ) return false;

    $count = get_transient( 'user_rank_comments_' . substr(md5( strtolower(get_comment_author_email( $comment_id )) ),0,8) );

    if ( false === $count ) {

        global $wpdb;
        $count = $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(comment_ID) 
                                                  FROM ' . $wpdb->comments. ' 
                                                  WHERE comment_author_email = %s
                                                    AND comment_approved = 1'
                                                    , 
                                                    get_comment_author_email()

                                                ) 
                                        );
        set_transient( 'user_rank_comments_' . substr(md5( strtolower(get_comment_author_email( $comment_id )) ),0,8), $count );
    } 


    foreach( $ranks as $row ) {
        if( $count >= (int)$row['count'] )
            $rank = $row['name'];
        else break;
    }
    return $rank;
}
    }
  • I don't have this plugin, but It looks like get_user_rank function is what prints the user rank. So if you want to change what is printed, you will need to modify that function. You can modify that function using filters. See codex on how to use filters: codex.wordpress.org/Function_Reference/add_filter – gdaniel May 15 '13 at 14:21
0

From inside a comment Loop...

$author_data = get_user_by('login',$comment->comment_author);
if (!empty($author_data)) {              
  var_dump($author_data->roles);
}

$author_data->roles is an array so you will need to work out what you want to do with that. That is, print them all? Print the highest ranking role?

if (array_intersect(array('administrator','moderator'),$author_data->roles)) {
  echo 'admin';
} elseif (array_intersect(array('something','else'),$author_data->roles)) {
  echo 'something else';
}
  • I want to echo 'admin' instead of the ranking if the user is an admin, moderator or some other think. Yes that's it in other words but I can't see how your code would help me. – Strasbourg May 15 '13 at 14:53
  • You want to echo something if a user has some particular role, or roles, and you don't see how finding out what the user's role is or roles are would help? Once you've got the role map it to whatever text you want. I'll edit the answer with an example. – s_ha_dum May 15 '13 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.