-2

I have a ticket support form on my site which right now has a field which returns (in the admin area) the name of the person who submitted the form.

Anyone know how I would modify this to display their user role instead? ie. Subscriber, Editor, etc.

$raised_by='';
if($ticket->type=='user'){
    $user=get_userdata( $ticket->created_by );
    $raised_by=$user->display_name;
}

I'm guessing it'll be something with this stuff in it...but I'm not too savy when it comes to this.

function get_user_role() {
    global $current_user;
    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);
    return $user_role;
}
edit7279
  • 175
  • 5
  • 15

2 Answers2

3

Please change last line of your code to this:

$raised_by=ucwords($user->roles[0]);

So that your current code which display First Name i.e.

    $raised_by='';
       if($ticket->type=='user'){
           $user=get_userdata( $ticket->created_by );
           $raised_by=$user->display_name;
          }

Above code will become:

 $raised_by='';
       if($ticket->type=='user'){
           $user=get_userdata( $ticket->created_by );
           $raised_by=ucwords($user->roles[0]);
          }

Update: To remove underscore with space your code may become as:

$raised_by='';
       if($ticket->type=='user'){
           $user=get_userdata( $ticket->created_by );
           $raised_by= ucwords(str_replace("_"," ",$user->roles[0]));
          }

You may notice, I have added ucwords function of PHP also, it is to make sure , roles on the screen look good, i.e. admin will be shown as Admin etc.

Also you may notice roles[0], 0 means that data currently we have there is as an array. So we are picking the first user roles from all the roles assigned to the user. I am sure it will be sufficient for your needs.

Let me know if this solves your issue or you still need any help. You can post in comments. Or Update your question.

Muhammad Asadullah
  • 3,735
  • 1
  • 22
  • 38
  • Works! You're a life saver. One last thing. It's displaying the user role with the standard underscore WP puts in, is there a way around that? For example, "Basic_subscriber" I would prefer be displayed as "Basic Subscriber" Not a huge deal, no one sees it but me, but just wondering... – edit7279 Oct 12 '15 at 18:01
  • @bjkralik Happy to know it worked for you! You need something like this $raised_by = str_replace("_"," ",$raised_by); then you may again use ucwords for both words to make captial letter. Wait i update above code. – Muhammad Asadullah Oct 12 '15 at 18:03
  • Sorry, could you update the last line of your code above to reflect what you mean. – edit7279 Oct 12 '15 at 18:10
  • Great! @bjkralik You may pick my answer for the question to make it resolved. Thank you! – Muhammad Asadullah Oct 12 '15 at 18:25
0

You could use this line of code.

$raised_by='';
if($ticket->type == 'user'){
    $user = get_userdata( $ticket->created_by );
    $raised_by = implode(', ', $user_info->roles);
}

Or, if you prefer to use the get_user_role function that you've written, slightly modify it to take the user ID as input and return the user role.

function get_user_role($user_id) {
    $user_info = get_userdata($user_id);
    $user_roles = $user->roles;
    $user_role = array_shift($user_roles);
    return $user_role;
}

You could use it like as shown below to output the user role.

$raised_by='';
if($ticket->type == 'user'){
    $user = get_userdata( $ticket->created_by );
    $raised_by = get_user_role($user->ID);
}
Hareesh Sivasubramanian
  • 1,265
  • 7
  • 17
  • 27