Advertisement
Aurangajeb

Show list of users on the frontend

Jul 10th, 2021
1,990
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1. /**
  2. * Show list of users on the frontend
  3. * // https://wpcrumbs.com/how-to-list-users-using-wp-user-query-and-shortcodes/
  4. **/
  5.  
  6. function show_users_lists($atts) {
  7.     // I don't use shortcode_atts because I want to have a dynamic number of attributes
  8.     $query = new WP_User_Query($atts);
  9.     $results = $query->get_results();
  10.  
  11.     // no results
  12.     if (empty($results)) {
  13.         return;
  14.     }
  15.    
  16.     // when we have results
  17.     ob_start();
  18.     echo '<ul>';
  19.     foreach ($results as $item) {
  20.     ?>
  21.         <li>
  22.             Name: <a href="<?php echo get_author_posts_url($item->ID); ?>">
  23.                 <?php echo esc_html($item->display_name); ?>
  24.             </a>
  25.             <br />
  26.             <?php echo 'Email: ' . esc_html($item->user_email); ?>
  27.             <br />
  28.         </li>
  29.     <?php
  30.     }
  31.     echo '</ul>';
  32.     return ob_get_clean();
  33. }
  34.  
  35. add_shortcode('user_lists', 'show_users_lists');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement