Creating Roles

Sometimes, rather than adding capabilities to an existing role, you need to create a whole new role. The code to do so is relatively easy. Again, place the lines in Listing 10-7 in your theme's functions.php file.

Listing 10-7. Creating a new role

<?php add_role('privatereader', 'Private Reader', array( 'read' => 1, 'level_0' => 1, 'read_private_pages' => 1, 'read_private_posts' => 1,

The add_role() function requires three arguments: the role's name (for internal use), the display name, and an array of the role's capabilities. (See for a complete list of the capabilities in WordPress.) Here, you've given the role the same two capabilities a subscriber starts out with, 'read' and 'level_0.' (The level_n capabilities exist for backward compatibility; very early versions of WordPress used a 1-10 scale instead of named roles.) Then, you've added the two roles relating to private content.

The functions that check for the presence of a capability in the role array will test for Boolean values. You could use true or false, but most WordPress developers prefer to use 1 or 0.

That's all it takes! Now, if you add a new user or edit an existing one, your role dropdown should include the Private Reader option, as shown in Figure 10-15.

If you want to modify your new role, use the same code as before, but change the get_role() function's argument to your role's internal name, as shown in Listing 10-8.

Listing 10-8. Modifying the new role

// remove ability to read private pages $PrivateRole = get_role('privatereader');

$PrivateRole -> remove_cap('read_private_pages'); ?>

In this example, it makes sense to create a new role only if you want to keep the subscriber role as-is; otherwise; it's simpler to just modify the subscriber role. When would it make sense to create a new role?

Email Marketing Mastery

Email Marketing Mastery

Discover The Exact Methods For Writing Highly Responsive Emails And Start Making More Sales From Your List Starting Today! If you're not using these tactics, you could be wasting your time building a list! Do you get frustrated when you build your list but don't make any sales from it? Do your subscribers actually look forward to receiving your emails each day?

Get My Free Ebook

Post a comment