Display list of members on the frontend

I want to show a list of members of a specific membership plan on the frontend. How do I do this?

  • Ash
    • WordPress Hacker

    Hello Turnpiece

    Please try the following code:

    add_shortcode( 'show_members', 'show_members_cb' );
    function show_members_cb( $atts ) {
        $atts = shortcode_atts(
            array(
                'id' => 0
            ),
            $atts
        );
        if( $atts['id'] == 0 ) {
            return 'You need to provide a membership ID';
        }
        $args = array(
            'membership_id' => $atts['id'],
            'subscription_status' => 'active'
        );
        $members = MS_Model_Member::get_members( $args );
        $html = '<table cellpadding="5" cellspacing="5">';
            $html .= '<tr>';
                $html .= '<th>User ID</th>';
                $html .= '<th>Username</th>';
                $html .= '<th>Display Name</th>';
            $html .= '</tr>';
        foreach( $members as $member ) {
            $html .= '<tr>';
                $html .= '<td>' . $member->id . '</td>';
                $html .= '<td>' . $member->username . '</td>';
                $html .= '<td>' . $member->name . '</td>';
            $html .= '</tr>';
        }
        $html .= '</table>';
        return $html;
    }

    You can use this code in your theme's functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    And then use the shortcode like this:

    [show_members id="1234"]

    Where 1234 is the membership ID.

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.