0

I am stumped on something I have been working on and would really appreciate any help you can offer.

The code below is what elements are to be displayed on a job note. It currently has the users avatar, username, date created and the comment. Both users and administrators(co-workers) can use the notes. If the username of the comment is Administrator then next to it display "- expert". But if the username of the comment is not Administrator role then it will not display anything next to it.

<?php ?><div itemscope itemtype="https://schema.org/Comment">
<div class="gv-note-author-details" itemscope itemtype="https://schema.org/Person">
    <div class="gv-note-avatar" itemprop="image">{avatar}</div>
    <h6 class="gv-note-author" itemprop="name">{user_name} <?php if { echo "Echo expert title here";} else { "echo nothing"}}?></h6>
</div>
<span class="gv-note-added-on" itemprop="dateCreated" datetime="{date_created}">{added_on}</span>
<div class="gv-note-content" itemprop="comment">{value}</div>

The only thing I see online that references this is get_current_user but I don't want it to get current user. I want it to simple check if username is Administrator then echo "- expert"; else echo "echo nothing";

Note {user_name} is display name of user

closed as off-topic by Max Yudin, Nathan Johnson, Christine Cooper, Dave Romsey, Jack Johansson Feb 28 '18 at 11:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • What's the connection with WordPress? – Max Yudin Feb 15 '18 at 19:22
  • In case this is helpful here is the file that determines what {user_name} is: codex.gravityview.co/… – Kevin W. Feb 16 '18 at 17:37
  • 1
    Gravity Forms is not a part of WordPress. The syntax you use is not WordPress' own also. – Max Yudin Feb 18 '18 at 11:13
2

@terminator answer might work but there's no need to create an array and use array_intersect I think since you're only looking for one role. This might be more comprehensible:

$current_user = wp_get_current_user();
$roles = (array) $current_user->roles;
if (in_array('administrator', $roles) { /* echo whatever */ }

Don't forget to check that 'administrator' is acutally the role key you want.

  • In case this is helpful here is the file that determines what {user_name} is: codex.gravityview.co/… – Kevin W. Feb 16 '18 at 17:37
  • Sorry I don't see the link between your question and GravityView ? And do you have additional questions ? – BBackerry Feb 17 '18 at 19:49
1

Both Bbackerry and terminator didn't read properly. The OP asked: "I want it to simple check if username is Administrator then echo "- expert"; else echo "echo nothing";". Which is the following code:

$current_user = wp_get_current_user();
if ( $current_user->user_login == 'Administrator' ) {
    echo '- expert';
} else {
    echo "echo nothing";
}
  • Hey Beee, I tested it and it didn't work :/ It was just echoing "echo nothing" everytime. Would I need to add a get_currentuserinfo(); in there somewhere? – Kevin W. Feb 16 '18 at 10:19
  • To be honest, I never use wp_get_current_user, I always use get_userdata( get_current_user_id() ). get_currentuserinfo is depracated. BUT it is better to check if a user has certain capabilities instead of checking for a username, since a username can be changed and then your code doesn't work anymore. – Beee Feb 16 '18 at 12:46
  • Please keep in mind the comparison for user name is caps sensitive. – Beee Feb 16 '18 at 15:48
  • The thing I still see wrong is the snippet you provided checks the if the current user is logged in correct? But there is nothing comparing it to the (user_name}. I am still very new to backend stuff as I am a frontend guy but Im wondering if there is a way to make it like this so that get current user could work: $current_user = wp_get_current_user(); If user name = administrator and is current user logged in then echo something else echo nothing. Does that make sense? – Kevin W. Feb 16 '18 at 17:17
  • In case this is helpful here is the file that determines what {user_name} is: codex.gravityview.co/… – Kevin W. Feb 16 '18 at 17:37
0

I think this might work for you

$username = "terminator";
$user = get_user_by('login',$username); 
if(in_array('administrator',$user->roles )){    
    echo "Echo expert";
}else{
    echo "nothing";
}

Let me know how it goes thanks

  • Hey Terminator, as Beee mentioned above I am not trying to get current user I am trying to check is {user_name} is role administrator or not. If you have any ideas on that it would be much appreciated! – Kevin W. Feb 16 '18 at 10:20
  • In case this is helpful here is the file that determines what {user_name} is: codex.gravityview.co/… – Kevin W. Feb 16 '18 at 17:37
  • @Kevin W. Please see the update answer – terminator Feb 16 '18 at 19:26
  • Okay I see where the confusion is! So the code I pasted above echo's {user_name} so the user's display name of who created the note. The script is the template for the note so the information changes based on what was entered. I don't want to check to see if the users name is Administrator. I want to see if the array {user_name} is role Administrator and if it is then echo "- Expert" and if the user role is not Administrator and in this case Subscriber then echo nothing. Is something like that even possible? – Kevin W. Feb 16 '18 at 23:05
  • I feel really bad for taking up your time on this but I really need help with it! haha – Kevin W. Feb 16 '18 at 23:05

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