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 created a group with the Groups plugin and I want to display the members list of that group. I put the shortcode [groups_group_info group="Group Name Here" show="users"] in a page, but it displays only the usernames of members, like this:

  • username1
  • username2
  • ...
  • usernameX

I want to display their names (display_name), but the plugin do not have this option (I have not found it). I opened a thread at the plugin support page, but I do not received an answer yet.

My question is: Is possible to change the usernames listed by that shortcode with their corresponding displayed names (or first - last name) + some user metadata?

Bellow is the original code that renders the shortcode [groups_group_info group="Group Name Here" show="users"]:

add_shortcode( 'groups_group_info', array( __CLASS__, 'groups_group_info' ) );

public static function groups_group_info( $atts, $content = null ) {
    global $wpdb;
    $output = "";
    $options = shortcode_atts(
        array(
            'group' => '',
            'show' => '',
            'format' => '',
            'single' => '1',
            'plural' => '%d'
        ),
        $atts
    );
    $group = trim( $options['group'] );
    $current_group = Groups_Group::read( $group );
    if ( !$current_group ) {
        $current_group = Groups_Group::read_by_name( $group );
    }
    if ( $current_group ) {
        switch( $options['show'] ) {
            case 'name' :
                $output .= wp_filter_nohtml_kses( $current_group->name );
                break;
            case 'description' :
                $output .= wp_filter_nohtml_kses( $current_group->description );
                break;
            case 'count' :
                $user_group_table = _groups_get_tablename( "user_group" );
                $count = $wpdb->get_var( $wpdb->prepare(
                    "SELECT COUNT(*) FROM $user_group_table WHERE group_id = %d",
                    Groups_Utility::id( $current_group->group_id )
                ) );
                if ( $count === null ) {
                    $count = 0;
                } else {
                    $count = intval( $count );
                }
                $output .= _n( $options['single'], sprintf( $options['plural'], $count ), $count, GROUPS_PLUGIN_DOMAIN );
                break;
            // @todo experimental - could use pagination, sorting, link to profile, ...
            case 'users' :
                $user_group_table = _groups_get_tablename( "user_group" );
                $users = $wpdb->get_results( $wpdb->prepare(
                    "SELECT * FROM $wpdb->users LEFT JOIN $user_group_table ON $wpdb->users.ID = $user_group_table.user_id WHERE $user_group_table.group_id = %d",
                    Groups_Utility::id( $current_group->group_id )
                ) );
                if ( $users ) {
                    $output .= '<ul>';
                    foreach( $users as $user ) {
                        $output .= '<li>' . wp_filter_nohtml_kses( $user->user_login ) . '</li>';
                    }
                    $output .= '</ul>';
                }

                break;
        }
    }
    return $output;
}
share|improve this question
    
have you looked at the code that renders the shortcode? if they don't provide any filters you could always duplicate it and create your own, changing what you need to. –  Milo Jan 25 '13 at 22:30
    
Yes, follow @Milo advice and you'll get there. Much probably anyone who wants to answer will do exactly that: open the code, copy the shortcode function, adapt. –  brasofilo Jan 25 '13 at 22:43
    
@Milo Thanks for the advice! I already adapted that code (I added it to my question), modifying the $user->user_login to $user->display_name, and things are fine now. But 1) for me is too complicated to make this function to return also some metadata for the listed users and 2) I do not want to edit the plugin itself, so this function must be moved somewhat to my theme functions.php. How to do this? Just copying? Can you help me? –  Iurie Jan 26 '13 at 8:21
    
@brasofilo I followed Milo advice but some things are too hard for me (see my previous comment). How to do them in the right way? –  Iurie Jan 26 '13 at 8:31
1  
lurie, yes, correct. Copy the code to create your own shortcode, removing the Class stuff and maybe even the switch, leaving inside the if($usergroup) only what's contained in case 'users'. So, you'll have [my_groups group="Group Name Here"]. –  brasofilo Jan 26 '13 at 15:13

1 Answer 1

up vote 0 down vote accepted

Bellow is the adapted function (by 24 rows shorter than the original), as was suggested by the commenters (many thanks!). It will work only if the Groups plugin was installed and activated.

add_shortcode( 'groups_group_info', 'groups_group_info' );

function groups_group_info( $atts, $content = null ) {
global $wpdb;
$output = "";
$options = shortcode_atts(
    array(
        'group' => '',
        'show' => '',
        'format' => '',
        'single' => '1',
        'plural' => '%d'
    ),
    $atts
);
$group = trim( $options['group'] );
$current_group = Groups_Group::read( $group );
if ( !$current_group ) {
    $current_group = Groups_Group::read_by_name( $group );
}
if ( $current_group ) {
            $user_group_table = _groups_get_tablename( "user_group" );
            $users = $wpdb->get_results( $wpdb->prepare(
                "SELECT * FROM $wpdb->users LEFT JOIN $user_group_table ON $wpdb->users.ID = $user_group_table.user_id WHERE $user_group_table.group_id = %d",
                Groups_Utility::id( $current_group->group_id )
            ) );
            if ( $users ) {
                $output .= '<ul>';
                foreach( $users as $user ) {
                    $output .= '<li>' . wp_filter_nohtml_kses( $user->display_name ) . '</li>';
                }
                $output .= '</ul>';
            }
}
return $output;
}
share|improve this answer

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.