The idea for this came from a request, where the customer wanted to allow users to register online, and then allow these users to be able to view a list of all other users that have registered. Regular site members, such as subscribers, editors, authors or contributors however should NOT be included, and neither should these (standard WordPress roles) have access to the page that lists these “special” users.

It can be complicated to list users by Role in WordPress. There is no API or function in WordPress to create a subset of users based on their role. All (or most) examples I could find was to use the user-level assigned to each Role, but since that is deprecated since WordPress 2.0, I didn’t want to use that.

Rather than coding the entire functionality, I tried to use as much of the available tools, plugins and WordPress functions available. The solution I came up with consists of 2 plugins, a bit of configuring, and a bit of coding. All tied together it makes a nice way to implement additional CMS functionality in WordPress, without using a full membership plugin.

Note, this is not a “download and run” solution, but if you follow the steps, it will work, and hopefully will also give you an idea of what can be achieved with WordPress using plugins, a bit of code and your imagination.

The tools required

Using and configuring the Member plugin

Why use the Member plugin

The member plugin by WordPress theme and plugin developer Justin Tadlock allows us (amongst others) to add a new role to the existing ones in WordPress. By adding a new role, and new capabilities for that role, we will be able to list users that have a specific role, thus filtering out the “regular” site users, those having another Role.

Get the Member plugin

Download the plugin from the WordPress Plugin directory and install/activate.

Configure the Member plugin

Once activated, you can find a link to the Member Components settings in the Settings menu of your WordPress dashboard. First we need to “activate” the features we need, so tick the first three options:

  • Edit Roles
  • New Roles
  • Content Permissions

Click activate, and new options will appear in the Users menu on the dashboard. We can now add and configure the settings.

  • Allow admin to manage Roles – First of all, we need to assign permissions to a user to be allowed to configure and maintain the new Role settings using the Member plugin. So, assuming we’re logged in as an administrator user, select the Roles option from the User menu, and assign (tick) the following capabilities: delete_roles, edit_roles, create_roles and restrict_content:
  • Define a new Role – Add a Role, e.g. sponsor, and a Role Label, e.g. New sponsor. The “read” capability is the only one ticked, so users from the Sponsor group will initially have the “regular” subscriber capabilities (permissions).
  • Define a new Capability, specific to that Role – Go to the Roles menu again, hover over the newly created Role, and select the “edit” option. On the edit Role panel, at the bottom, find the New Capabilities fields. You can add up to six custom capabilities for a Role. Add the new capability “is_sponsor”:

Using and configuring the Simplr registration plugin

Why use the Simplr registration plugin

The plugin allows us to add a registration page, and assign a role of our choice, overriding the WordPress default user role. A confirmation e-mail will be sent to users that signup through this page.

Get the Simplr registration plugin

Download the plugin from the WordPress Plugin directory and install/activate.

Configure the Simplr registration plugin

Once activated, the plugin will add an option to the Settings Menu in WordPress. Configure the e-mail settings, and you’re good to go.


Create the code to list users by Role

As I explained earlier, one can not select users by Role (easily), so here’s the little trick that will allow you to accomplish the same. You can select users by Capabilities using a standard WordPress API.

The following block of code may be a bit overwhelming, but what it does is, as documented in the code:

  • retrieves a list of all blogusers
  • loops through this list, and checks the user’s capabilities
  • if a user has the capability is_sponsor (and thus has the Role Sponsor), the user data will be printed
  • finally, a shortcode for this function will allow us to use this function in a WordPress page or post
[php] function get_sponsors() {

// Get all Registered users
$blogusers = get_users_of_blog();

// If there are any users
if ($blogusers) {
foreach ($blogusers as $bloguser) {

// Get user info
$user = new WP_User($bloguser->user_id);

// check whether the user has the capability to view this page, if so, continue
if ($user->has_cap(‘is_sponsor’) ) {

// display avatar (48px square)
echo get_avatar($user->user_email, $size = ’48’);

// output other user data, if populated
echo(‘Display Name: <a href=\"’.$user->user_url."\">".$user->display_name."</a><br />\n");
echo(‘Username: ‘ . $user->user_login . "<br />\n");
if ($user->user_nicename) { echo(‘User Nice Name: ‘ . $user->user_nicename . "<br />\n"); }
if ($user->user_email) { echo(‘User e-mail: ‘ . $user->user_email . "<br />\n"); }
if ($user->first_name) { echo(‘First Name: ‘ . $user->first_name . "&nbsp;"); }
if ($user->last_name) { echo($user->last_name . "<br />\n"); }
if ($user->nickname) { echo(‘Nickname: ‘ . $user->nickname . "<br />\n"); }
if ($user->description) { echo(‘Bio: ‘ . $user->description . "<br />\n"); }
echo "<hr />";
}
}
}
}

// Add a shortcode to this function so it can be used in posts and pages
add_shortcode(‘list-sponsors’, ‘get_sponsors’);
[/php]

Create the required Pages on your site

We now need to add two pages to our site. One page that will allow user to register as a sponsor, and one page to list all the sponsors.

  • Create a “registration” page – Write a new page, and name it whatever is appropriate, e.g. “Sponsor Registration” and add the following content:
    [Register role="sponsor"], and any other content or directions that you wish to supply. In this particular case, the Biographical Info field from the users menu may be a good place to allow the sponsors to add additional information about themselves.
  • Create a “list sponsors” page – Write another page, and include the shortcode we created in the previous step, to list all Users with the Sponsor Role: [list-sponsors]. Then, scroll down the write page panel, and find the Content Permission settings. Tick Administrators and Sponsors to restrict the content to these users only, thus making sure that the Sponsor list is only accessible to users that have registered as Sponsors.

Final Words

Although this is a very specific implementation, I hope to have demonstrated that using tools (plugins) readily available you can add all sorts of functionality to your site, create usergroups, restrict content and such. It will allow you to extend the CMS capabilities of WordPress.

10 comments

  1. […] How to register and list users based on user roles in WordPress :: van Weerd. Subscribe To Site: Full Post Feed | Summary Feed | Comments Feed Share and […]

  2. Alex

    DUDE THANK YOU!!!!
    IT WORKED, I being looking for this for so long!!!!

  3. Dwaynne

    Tried to modify the code a bit with my own field names and added it to functions.php/ Blew up my WordPress pages area on the Dashboard.

    Added this:
    user_id);

    // check whether the user has the capability to view this page, if so, continue
    if ($user->has_cap(‘list_trinidad’) ) {

    // display avatar (48px square)
    echo get_avatar($user->user_email, $size = ’48’);

    // output other user data, if populated
    echo(‘Display Name: user_url.”\”>”.$user->display_name.”\n”);
    echo(‘Username: ‘ . $user->user_login . “\n”);
    if ($user->user_nicename) { echo(‘User Nice Name: ‘ . $user->user_nicename . “\n”); }
    if ($user->user_email) { echo(‘User e-mail: ‘ . $user->user_email . “\n”); }
    if ($user->first_name) { echo(‘First Name: ‘ . $user->first_name . ” “); }
    if ($user->last_name) { echo($user->last_name . “\n”); }
    if ($user->nickname) { echo(‘Nickname: ‘ . $user->nickname . “\n”); }
    if ($user->description) { echo(‘Bio: ‘ . $user->description . “\n”); }
    echo “”;
    }
    }
    }
    }

    // Add a shortcode to this function so it can be used in posts and pages
    add_shortcode(‘list-trinidadstaff’, ‘get_trinidad’);
    ?>

    And got this:

    Warning: Cannot modify header information – headers already sent by (output started at /home/virb/public_html/bicl2/wp-content/themes/bicl/functions.php:23) in /home/virb/public_html/bicl2/wp-includes/pluggable.php on line 890

    1. Ronald

      Hi,

      you should check the line that echoes the Display Name. That code is not valid.

      There are also 4 closing }’s …. 1 will do.

      1. Dwaynne

        Sorry, man. My code got chopped up badly, so only some of it came across when I posted the comment.

  4. Bofy

    Hi!
    For me didn’t word the code.
    My blog was totally blocked.
    I don’t know what to do.

    1. Bofy

      Sorry.
      A mistake:
      “didn’t word” to be replaced with “didn’t work”

  5. Esteban

    “…Then, scroll down the write page panel, and find the Content Permission settings. Tick Administrators and Sponsors to restrict the content to these users only, thus making sure that the Sponsor list is only accessible to users that have registered as Sponsors. ”

    Where I’m I supposed to find the “Content Permission settings”?

    I followed the example and it worked, but it’s only that part that isn’t clear to me.

    Thanks and great tutorial!

  6. Sandi

    The assignment of roles is not working. The user is created with Role – none.

Leave a Reply