X

How To Change User Role Capabilities

Snippets by IsItWP

Are you looking for a way to change user role capabilities? This snippet will let you change user role capabilities in order to create your own customized user roles.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin.

IMPORTANT: Capabilities are stored in the database, so if you want to give a capability back to a role after deleting it, you have to use and run the add_cap() function.

<?php
# 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');
?>

For more information about roles and capabilities, check out the WordPress Codex.

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: How to Create a Custom User Registration Form in WordPress.

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!