Support

Account

Home Forums General Issues Display Name and role using a user field

Solved

Display Name and role using a user field

  • Hi!
    I have a front-end form with one user field to select which users (“asistentes”) have attended an event. As a result, I get a list of those users, but I need to display, not only their names but also their registered user roles. How could I do it?

    This is the code I’m using:

    <h3>Asistentes</h3>
    <div>	
      <?php $user = get_field('asistentes'); ?>
    <ul>
    <?php foreach ($user as $user): ?>
    <li>
     <p><?php echo $user['display_name']; ?> - <?php $user_info = get_userdata($user_id);
          echo 'User roles: ' . implode(', ', $user_info->roles) . "\n";
    ?></p>
    </li>
    <?php endforeach; ?>
    </div>

    What am I missing?
    Thanks in advance!

  • If you are returning an array as indicated by your code echo $user['display_name']; then you should also use this syntax for roles echo 'User roles: ' . implode(', ', $user_info['roles']) . "\n";

  • Thank you John for your answer!
    You’re right, the field returns an array.

    This is the code I’ve used, then:

    <?php foreach ($user as $user): ?>
    <li>
     <p><?php echo $user['display_name']; 
              echo 'User roles: ' . implode(', ', $user_info['roles']) . "\n";?>
     </p>
    </li>
    <?php endforeach; ?>

    And got this error message:
    Warning: implode(): Invalid arguments passed in /home/volunta6/staging/1/wp-content/plugins/acf-enhanced-message-field/acf-enhanced-message-v5.php(56) : eval()’d code on line 38

    Any further help on this would be very appreciated!

  • I see part of your problem

    
    $user = get_field('asistentes');
    foreach ($user as $user):
    

    Not sure if you see it or not
    Try this

    
    $users = get_field('.....
    foreach ($users as $user)
    
  • John,
    As you may have already noticed, I’m very amateur on this 🙁
    I’ve made that change and get the same error message.

    Here’s the code:

    <h3>Asistentes</h3>
    <div>	
      <?php $users = get_field('asistentes'); ?>
    <ul>
    <?php foreach ($users as $user): ?>
    <li>
     <p><?php echo $user['display_name']; 
          echo 'User roles: ' . implode(', ', $user_info['role']) . "\n";?></p>
    </li>
    <?php endforeach; ?>
    </div>

    This is line 38, which triggers the error:
    echo 'User roles: ' . implode(', ', $user_info['role']) . "\n";?></p>

    Any further solution?
    Thanks a lot!

  • For implode() to work, the second argument has to be an array, but I do not see the variable $user_info set anywhere.
    Now, since there could be anything behind that variable, please dump it and post what it spits out, provided that it does not reveal any sensitive information:

    <h3>Asistentes</h3>
    <div>
    <?php $users = get_field('asistentes'); ?>
    <ul>
    <?php
    foreach ($users as $user):
    var_dump( $user , $user_info );
    ?>
    <li>
    	<p><?php echo $user['display_name'];
    	echo 'User roles: ' . implode(', ', $user_info['role']) . "\n";?></p>
    </li>
    <?php endforeach; ?>
    </div>
  • Thanks Daniel for your help!
    After dumping it, this is what I get (attached image)
    “Role” does not appear… and I don’t know where to find it.

    I also tried this code, getting the role from the user ID (112 is the id of a user on the list) and it works, but I don’t know how to pass the user ID as a parameter.

    <h3>Asistentes</h3>
    <div>	
      <?php $users = get_field('asistentes'); ?>
    <ul>
    <?php foreach ($users as $user): ?>
    <li>
     <p><?php echo $user['display_name']; ?> - <?php $user_info = get_userdata(112);
          echo 'User roles: ' . implode(', ', $user_info->roles) . "\n";
    ?></p>
    </li>
    <?php endforeach; ?>
    </div>

    I think I’m getting closer… What am I missing?
    Thanks in advance!

  • If $user['display_name'] works just fine, then perhaps this will to do the trick. At least it should:

    <h3>Asistentes</h3>
    <div>
    <?php $users = get_field('asistentes'); ?>
    <ul>
    <?php
    foreach ($users as $user):
    $roles = new WP_User( $user['ID'] ) ?? false ;
    $roles = is_object( $roles ) ? $roles->roles : [] ;
    ?>
    <li>
    	<p><?php echo $user['display_name'];
    	echo 'User roles: ' . implode(', ', $roles) . "\n";?></p>
    </li>
    <?php endforeach; ?>
    </div>
  • Thanks for the approach,
    it doesn’t show anything 🙁

  • Wow, that was quick. A few minutes after having sent that post I noticed an error and corrected it.
    Please confirm that you have applied exactly what my previous post shows now.
    If you have done so and it did not work, please try this next and show me the dumped values from at least one iteration of that foreach. We are indeed getting closer:

    <h3>Asistentes</h3>
    <div>
    <?php $users = get_field('asistentes'); ?>
    <ul>
    <?php
    foreach ($users as $user):
    $roles = new WP_User( $user['ID'] ) ?? false ;
    var_dump( $user , $roles );
    $roles = is_object( $roles ) ? $roles->roles : [] ;
    var_dump( $roles );
    ?>
    <li>
    	<p><?php echo $user['display_name'];
    	echo 'User roles: ' . implode(', ', $roles) . "\n";?></p>
    </li>
    <?php endforeach; ?>
    </div>
  • Yes, I applied the exact code you sent.

    Here are the dumped values I get, the one I’d like to show next to the name is “voluntario”.

    Thanks a lot for your help!

  • I should have encapsulated the damn thing in <pre>...</pre>. Would have been much easier to read.
    Now, the dump says that this should, in fact, have been the solution. But if the roles still do not show up, I must have overlooked something. Please try the following and, when you censor the sensitive data, take care to not paint over any brackets. Those are important.

    <h3>Asistentes</h3>
    <div>
    <?php $users = get_field('asistentes'); ?>
    <!--<ul>-->
    <?php
    foreach ($users as $user):
    $roles = new WP_User( $user['ID'] ) ?? false ;
    echo '<pre>';
    var_dump( $user , $roles );
    $roles = is_object( $roles ) ? $roles->roles : [] ;
    var_dump( $roles );
    echo '</pre>';
    ?>
    <li>
    	<p><?php echo $user['display_name'];
    	echo 'User roles: ' . implode(', ', $roles) . "\n";?></p>
    </li>
    <?php endforeach; ?>
    </div>
  • Hi Daniel!
    I’m sorry, I’ve been out of the office for the last couple of days and didn’t answer your last reply.

    Your code works like a charm, it shows name + roles the way I needed.

    Thank you so much for your patience and help!
    Best regards!

  • No worries, I am not on the run. Glad I could help.
    I would appreciate it though, if you could give me the thumbs up for “case solved”.

  • I think I’ve set it on my last post by mistake and don’t know how to change it. So sorry… Could any Admin user change it, please?

  • Daniel,
    now you have your earned recognition with the ‘case solved’ badge.
    I’ve managed the change with the developer.
    Thanks again for your help!

Viewing 16 posts - 1 through 16 (of 16 total)

The topic ‘Display Name and role using a user field’ is closed to new replies.