Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question
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 '13 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? –  Iurie Jan 26 '13 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 '13 at 23:09
    
@brasofilo I updated my question, also added a draft code that I can't finish. Please, help! –  Iurie Jan 26 '13 at 23:25

1 Answer 1

up vote 2 down vote accepted

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;
}
share|improve this answer
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 '13 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 '13 at 21:00
    
@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! –  Iurie Feb 2 '13 at 21:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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