How to add File Upload Capabilities to the Contributor Role in WordPress

- by

I never noticed this before, but it appears that the Contributor Role in WordPress does not come with ability to upload files to an instance. That’s slightly weird, given that most users will probably assign this role to users who shall be able to write content for a site, and with good content inevitably come images in this day and age.

According to WordPress, the roles included with WordPress are mere examples or “capability collections” so to speak, and we’re free to create our own, or add/remove capabilities from any role if we so desire. I did some digging and found out how that the add_cap() and remove_cap() functions do just that.

In this article I’ll show you how to add the upload_files() capability to the Contributor Role, without the need for yet another plugin.

In principle, we can pick any of these capabilities and add/remove them to/from any role we like. The snag is though that doing so will make the change permanent in the database. Hence, once added, it’ll stay like this until we manually undo the change again. This sounds messy, so it’s best to run any such operation only once in a controlled manner, perhaps during theme/plugin activation.

I’ve decided to write a small function that tests if a theme is activated, and if so, add our upload capability. If the theme is deactivated, it’ll remove the capability again.

// add file uploads to Contributor role
function add_uploads_to_contribs () {
global $pagenow;
// grab contribtor role
$role = get_role ('contributor');
// if this theme is activated, add the upload capability
if ('themes.php' == $pagenow && isset ($_GET['activated'])) {
$role -> add_cap ('upload_files');
} else {
// remove when deactivated
$role -> remove_cap ('upload_files');
}
}
add_action ('load-themes.php', 'add_uploads_to_contribs');
view raw functions.php hosted with ❤ by GitHub

This Codex Article explains the process of adding a capability in principle, using a different example. I’ve modified it and came up with the code above. Add it to your theme’s functions.php file, preferably prepending the function name with your own unique prefix. Activate another theme briefly and the capability will be added.

Feel free to grab this code from GitHub and share it it friends, family and total strangers ?



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.