How to Customize WordPress Roles and Capabilities Without Hacking the Core

Here’s a handy tip for site admins who want to customize what user roles can and cannot do. The best thing is that you can make these changes without hacking the WordPress core. You can add the changes directly to your functions.php file. Here are a few examples, courtesy of wp-snippets.com:

1
2
3
4
5
6
7
8
# Get the user role #
$edit_contributor = get_role('contributor'); 

# Contributor can upload media #
$edit_contributor->add_cap('upload_files')

# Contributor can no longer delete posts #
$edit_contributor->remove_cap('delete_posts');

It’s important to note here that these capabilities are written to the database. If you need to give back a capability to a role, you have to use the add_cap-function.

Tags

Comments (3)

  1. Hi there,

    I did this and it completely crashed my site!!

    I added this to the functions.php file just before the final ?>:

    $edit_contributor->add_cap(‘upload_files’);
    $edit_contributor->remove_cap(‘delete_posts’);

    and then got a fatal error. I couldn’t get back to my wordpress dashboard or my site at all.

    Eventually after some panic I logged onto the host server (which I have virtually never done) with some difficulty found the functions.php file and removed the 2 lines.

    The site now works again.

    I’d really like this to work and have no idea why it caused my site to crash – any ideas?

    thanks,
    Alan

Participate