4

I have in the database a table with the user_id and corresponding group_id values, so each row contain the ID of the user that is a member of a group and the related group ID. The question is: How to display the names of users from a specific group, plus some metadata, with a shortcode, for example [group_members group_id=1]? With the bellow function, that I try to adapt for this without success, I can to display only the user ID or the group ID.

add_shortcode( 'group_members',  'group_members_shortcode_handler' );

function group_members_shortcode_handler ( $atts, $content = null ) {    

  global $wpdb;
  $querystr = "SELECT * FROM wp_groups_user_group";
  $users = $wpdb->get_results($querystr, OBJECT);

  foreach ($users as $user) {
    $output .= $user->group_id;
  }

 return $output;
}
4
  • 1
    There's no enough information to provide an Answer (unless someone is familiar with this plugin). Hopefully, you already read the documentation and checked the code, right?
    – brasofilo
    Jan 26, 2013 at 20:56
  • @brasofilo What information is needed? I can make the question more general, so not related to the Groups plugin. In this case we know only the table name (wp_groups_user_group), columns names (user_id and group_id). This is not enough?
    – Yurié
    Jan 26, 2013 at 22:10
  • Did you tried their support? They also have an API. Otherwise, try to build a shortcode with a custom $wpdb.
    – brasofilo
    Jan 26, 2013 at 23:09
  • @brasofilo I updated my question, also added a draft code that I can't finish. Please, help!
    – Yurié
    Jan 26, 2013 at 23:25

1 Answer 1

3

Below is my own answer to the question. I will appreciate any constructive comment.

add_shortcode( 'group_members',  'group_members_shortcode_handler' );

function group_members_shortcode_handler ( $atts, $content = null ) {    

  global $wpdb;
  $querystr = "SELECT * FROM wp_groups_user_group";
  $users = $wpdb->get_results($querystr, OBJECT);

  $output='';
  foreach ($users as $user) {
    if($user->group_id == $atts['group_id']){
      $firstName = esc_html(get_user_meta($user->user_id, 'first_name', true));
      $lastName = esc_html(get_user_meta($user->user_id, 'last_name', true));
      $output .= '<li>' . $firstName . ' ' . $lastName . ' - ' . esc_html(get_user_meta($user->user_id, 'teaching_position', true)) . '</li>' . PHP_EOL;
    }
  }

  return $output;
}
3
  • 1
    Looks ok, glad you sorted this out! Maybe, as good practice, declare $output=''; before the foreach loop. And you could grab all user meta at once.
    – brasofilo
    Jan 28, 2013 at 19:59
  • Oh, as we can see in that other Q&A, do an esc_html in all get_user_meta. :::: I'll comment over there about the specific all user meta issue.
    – brasofilo
    Jan 29, 2013 at 21:00
  • 1
    @brasofilo I updated the code as you suggested. I changed also to esc_html the esc_attr for the last get_user_meta from foreach loop. I think the code now is OK. Thank you!
    – Yurié
    Feb 2, 2013 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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