A guide to the Members plugin

The Members plugin is meant to be a complete user, role, and content management plugin for WordPress. Its purpose is to give you fine-grained control over who has access to what.

Right now, it's in the early stages of development. It's my hope that this provides that true CMS experience that many users long for in WordPress. There's a long road ahead, and I hope you join me for the ride.

The plugin documentation

Use the links below to navigate to a specific section in the documentation:

The relationship of users, roles, and capabilities

This is the most important thing to understand with this plugin. It's so important that I took the time out of my day to write a complete tutorial on understanding this: Users, roles, and capabilities in WordPress. If you don't understand this concept, you won't understand what this plugin does. This is not a concept created by the plugin. This is how it's done in WordPress.

I highly recommend reading that blog post, but here's the short version:

How to install the plugin

  1. Uzip the members.zip folder.
  2. Upload the members folder to your /wp-content/plugins directory.
  3. In your WordPress dashboard, head over to the Plugins section.
  4. Activate Members.

How to use the plugin

This plugin is set up to have a components-based system. The reason for this is that I don't want to stick everyone with a bunch of features they don't need. There's no point in using the Edit Roles component if all you need is just a login widget and some shortcodes. So, it's a use-only-what-you-want system.

To add components, look for the Members Components link under your Settings menu while in your WordPress admin. When on the new page, you'll be able to select the components you want to use.

I recommend at least activating Edit Roles component. It is at the heart of this plugin, and many other components will likely require its use in some form.

Edit Roles Component

This component can be both a blessing and a curse, so I'm going to ask that you use it wisely. Use extreme caution when assigning new capabilities to roles. You wouldn't want to give Average Joe the edit_plugins capability, for example.

Edit Roles is the big daddy of all the components. It allows you to edit and assign new capabilities to existing roles.

You can find the settings page for this component under the Users menu. It will be labeled Roles. When clicking on the menu item, you'll get a list of all the available roles. From there, you can select a role to edit.

To delete a role, you must have the delete_roles capability. If you don't have that, you won't see a delete option on the Roles page.

It is important that you assign the capability of edit_roles to a role (the administrator role would be a good one). That way, only users with that particular capability can edit roles.

New Roles Component

The New Roles component allows you to create new roles. The menu item for this is located under the Users menu and is labeled New Roles.

You can only use this component (create new roles) if you have the create_roles capability. So, if you don't have that, you need to use the Manage Roles component to add that capability to the role you currently have.

Adding new roles is pretty straightforward. You need to input a Role (only use letters, numbers, and underscores), Role Name, and select which capabilities the new role should have. You can later manage this role using the Edit Roles component.

You can assign new roles to users from the Authors & Users screen in WordPress. This is nothing particular to the plugin and is a default part of WordPress. I believe you need the edit_users capability to do this (I'll have to check).

Content Permissions Component

The Content Permissions component will be the heart and soul of this plugin in the future. Right now, it only adds an additional meta box on the post/page edit screen. This meta box allows you to select which roles can view the content of the post/page. If no roles are selected, anyone can view the content.

You need the restrict_content capability to use this component. So, you'll need to add this capability to your role using the Edit Roles component.

Note that you'll see a Role custom field key and values when testing this component. This is for my personal testing only right now.

Shortcodes Component

There are several shortcodes that you can use in your post/page editor. These need some major testing right now, so please offer any feedback you can.

[access]

The [access] shortcode is for hiding content from particular roles and capabilities. You need to wrap your content when using this shortcode:

[access role="editor"]Hide this content from everyone but editors.[/access]

Parameters:

Note that capability and role parameters aren't used in conjunction. The code first checks for the capability (if input) then checks for the role (if input).

[get_avatar]

This shortcode is for showing a user's avatar through Gravatar. It should be used like so within your post/page editor:

[get_avatar id="30"]

Parameters:

[is_user_logged_in]

The [is_user_logged_in] shortcode should be used to check if a user is currently logged into the site. If not, the content will be hidden.

[is_user_logged_in]This content is only shown to logged-in users.[/is_user_logged_in]

This shortcode has no parameters.

[feed]

If you have content you only want to show to subscribers of your feed, wrap it in this shortcode:

[feed]This content will only be shown in feeds.[/feed]

This shortcode has no parameters.

[login-form]

The [login-form]shortcode produces a form for users to log into your site. More than likely, you'll want to use the Login Form widget for something like this. The shortcode should be used like so:

[login-form]

This shortcode has no parameters.

Template Tags Component

The Template Tags component gives you additional functions (i.e., template tags) to use within your WordPress theme.

current_user_has_role()

This template tag checks if the currently logged-in user has a specific role.

<?php if ( function_exists( 'current_user_has_role' ) && current_user_has_role( 'editor' ) ) { ?>
	Only users with the editor role can see this content.
<?php } ?>

has_role()

This function will check if a specific user (by ID) has the given role.

<?php if ( function_exists( 'has_role' ) && has_role( 'editor', 30 ) ) { ?>
	If the user with the ID or 30 has the editor role, this will be shown.
<?php } ?>

members_list_users()

The members_list_users() template tag works much like wp_list_authors(). This is also a widget called Users if you're using the Widgets component.

<?php if ( function_exists( 'members_list_users' ) ) { ?>

	<ul>
		<?php members_list_users( array( 'order' => 'ASC', 'orderby' => 'display_name' ) ); ?>
	</ul>

<?php } ?>

This function currently only takes in a few parameters, but I hope to work more in sometime in the future.

members_author_profile()

A template tag to be used within The Loop for showing the current author's avatar, name (linked to author archive), and bio. A good place to use this is in your single.php template after the post.

<?php if ( function_exists( 'members_author_profile' ) ) members_author_profile(); ?>

Widgets Component

The widgets component provides easy-to-use widgets for your site. They can be used in any WordPress widget area (provided by your theme). Currently, there's the Login Form and Users widgets.

Login Form widget

The Login Form gives you a login form. It's a mixture of a text widget and login form. It can also show your avatar.

It's pretty straightforward, but I'll provide more documentation later.

Users widget

The Users widget allows you to list users in any widget area. It's based off the members_list_users() function, so all of the parameters are the same.

Private Blog Component

The Private Blog component makes sure that only logged-in users can see anything on your site. If a user visits your site and is not logged in, they are immediately redirected to your wp-login.php (WordPress login) page.

Note that feeds are not currently blocked with this component, but it's likely they will be later with an introduction of a feeds component.

Checking if the current user has a capability

In plugins and your theme template files, you might sometimes need to check if the currently logged in user has permission to do something. We do this by using the WordPress function current_user_can(). The basic format looks like this:

<?php if ( current_user_can( 'capability_name' ) ) echo 'This user can do something'; ?>

For a more practical situation, let's say you created a new capability called read_pages. Well, you might want to hide the content within your page.php template by adding this:

<?php if ( current_user_can( 'read_pages ' ) ) { ?>
	<?php the_content(); ?>
<?php } ?>

Only users with a role that has the read_pages capability will be able to see the content.

Components API

The components API is for developing new components to use within the Members plugin. This API is meant to be used so that users can select which components they want to run. While it is possible to build something on top of the Members plugin without using the components API, this provides a way to jump start development and keep code clean and organized.

Creating a custom component

To create a custom component, you need to use the register_members_component() function in your plugin. You should wrap it within its own function.

function register_my_components() {

	register_members_component( array( 
		'name' => 'component_name', 
		'label' => __('Component Label', 'members'), 
		'callback' => 'component_callback_function', 
		'hook' => false,
		'description' => __('Add a description of your component.', 'members') 
	) );
}

Once you've done the above, you need to add your function to the members_register_components hook.

add_action( 'members_register_components', 'register_my_components' );

Checking if a component is active

If you want to check if a component is active (nice way to only load code if needed), you can use the is_active_members_component() function.

if ( is_active_members_component( $component_name ) ) {
	/* Load files or do something else. */
}

Getting a component

If you need to grab a component object, you can do so with the get_members_component() function.

$component = get_members_component( $component_name );

echo $component->name;
echo $component->label;
echo $component->description;
echo $component->callback;
echo $component->hook;

Adding new default capabilities

Your plugin/theme can add new capabilities to the Edit Roles component if needed. This will allow users to easily select the additional capabilities for whichever roles they choose.

add_filter( 'members_get_capabilities', 'my_plugin_new_caps' );

function my_plugin_new_caps( $capabilities ) {

	$capabilities[] = 'cap_name_1';
	$capabilities[] = 'cap_name_2';
	$capabilities[] = 'cap_name_3';

	return $capabilities;
}

Note that you need to respect the existing capabilities and return the original array.

Checking for capabilities

In WordPress, you can use the current_user_can() function to check if the current user has a particular capability. Since you don't know whether a user has this plugin installed, you might want to check first.

The members_check_for_cap() function (only use in admin) checks if any role has a particular capability. This can be useful in setting up something like admin menus. For example, you can set up a theme settings menu for users that have the edit_themes capability. But, if this plugin is installed and a user has the edit_my_theme capability, that'll be used instead.

if ( function_exists( 'members_check_for_cap' ) && members_check_for_cap( 'some_cap' ) ) {
	/* Do something if any role has the 'some_cap' capability. */
else {
	/* Do something for people without the plugin. */
}

Need the old user levels system?

Some plugins and themes might rely on the old user level system in WordPress. WordPress still has legacy support for these, but I'm hoping to continue phasing out the use of them.

By default, the levels aren't shown. They still exist, but are tucked away behind the scenes. If you need to control who has what level (levels are just capabilities), add this to your plugin or your theme's functions.php:

remove_filter( 'members_get_capabilities', 'members_remove_old_levels' );

Plugin support

I run a WordPress community called Theme Hybrid, which is where I fully support all of my WordPress projects, including plugins. You can sign up for an account to get plugin support for a small yearly fee ($25 USD at the time of writing).

I know. I know. You might not want to pay for support, but just consider it a donation to the project. To continue making cool, GPL-licensed plugins and having the time to support them, I must pay the bills.

Members is licensed under the GNU General Public License, version 2 (GPL).

This plugin is copyrighted to Justin Tadlock.

2009 © Justin Tadlock