Techniques » WordPress Create Custom Roles and Capabilities

So you want to add a custom Role to your WordPress site to add more flexibility when assigning users their role, what do you do? Simply add a role, with a few lines of code in your theme’s functions.php file, using WordPress’ add_role() function, as defined below.

1
add_role( $role_name, $display_name, $capabilities );

The example below adds a role of Video Manager with a custom cabability of ‘manage videos’.

1
add_role( 'video_manager, 'Video Manager', array( 'manage_videos' ) );

NOTE: Once you add this code to your functions.php file and view any page on your site to make the role addition, you can and should comment it out so that it doesn’t run on every page view.

Now, how do we use this new role with the new capability you ask? Simply use the WordPress current_user_can() function to check the user’s capability.

1
2
3
if ( current_user_can( 'manage_videos' ) ) {
    // let them manage those videos!
}

This technique can be combined with our WordPress Role Capability Restriction technique to add even more capabilities to your new custom role. Simply create your new role then use it in the get_role() function call detailed in our technique.

About Corey

Corey was born, raised, and still lives in CMCH, NJ (about 5 minutes outside of Wildwood). He loves the Jersey Shore, and has loads of sand in his shoes! He has been involved with computers and programming since first purchasing a Commodore 64 in 1984. Corey formally began a web design company in 2004, ever broadening his progamming languages and skills since. code

13 Responses to WordPress Create Custom Roles and Capabilities

  1. Peter Goes says:

    What do you mean with the note that the code should be comment out while make role additions?

    tnx, Peter

    • Corey says:

      Good question, I should have been clearer with those instructions. The “add_role( ‘video_manager, ‘Video Manager’, array( ‘manage_videos’ ) );” adds a role to WordPress by adding an entry to the database, and only needs to be run once, adding a double slash // before the code will comment it out. So once you add this code to your functions.php file, view any page on your site, then you should comment it out. Otherwise the add_role will be run on every page view unnecessary, even though the role will only be added once to the database. The resulting code will look like this: // add_role( ‘video_manager, ‘Video Manager’, array( ‘manage_videos’ ) );. I hope this clarifies my post.

      • Peter Goes says:

        Thank you for the reply. I see why you don’t want to run the code every page refresh.
        But isn’t there some kind of hook/action the where add_role should be put in?
        It seems like a bit of a hack this way.

  2. Peter Goes says:

    I looked it up in the doc’s, when add_role is called, it check’s if the role exists, if not, it put’s it in the database.
    http://core.trac.wordpress.org/browser/tags/3.0.5/wp-includes/capabilities.php
    line: 136
    So I think you can leave it uncommented.

  3. Geert says:

    Hi,

    Checking against a custom capability, like :
    if ( current_user_can( ‘manage_videos’ ) )
    does not work here in WP 3.0.1

    add_role seems to write the name of the ROLE to the wp_capabilities fields in wp_usermeta, not the name of the custom CAPABILITY.

    Came across this?

    Thx.

  4. anup says:

    thanks

  5. Trent says:

    This is a fairly old post – but for those of you concerned with server overhead, you may wish to create a little plugin that does this on “activation” – then just deactivate it.

    I do this occasionally when I have one off things to do.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>