Enabling Contributors to Upload Files: A Step-By-Step Guide for WordPress

Home » Snippets » Enabling Contributors to Upload Files: A Step-By-Step Guide for WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Managing a multi-author WordPress website can be challenging, especially when you need to control different user roles and capabilities. By default, WordPress doesn’t allow contributors to upload files, which can be an issue if you rely on content from various contributors. In this article, we will guide you on how to safely enable contributors to upload files to your WordPress site, providing a streamlined process and promoting efficiency.

					if ( current_user_can('contributor') && !current_user_can('upload_files') )
    add_action('admin_init', 'wpturbo_allow_contributor_uploads');

function wpturbo_allow_contributor_uploads() {
    $contributor = get_role('contributor');
    $contributor->add_cap('upload_files');
}
				

The code snippet is designed to give WordPress ‘contributors’ the capability to upload files. By default, ‘contributors’ in WordPress don’t have the right to upload files – this change requires a tweak that you can implement through this code snippet.

This can be beneficial where you want to allow contributors more freedom when they edit their posts, like adding images, directly uploading documents etc.

Let’s break down the code:

First, the code checks whether the user is a contributor and doesn’t have ‘upload_files’ capability.

if ( current_user_can('contributor') && !current_user_can('upload_files') )

The ‘current_user_can’ function is utilized to examine the role and permissions of the active user. If the current_user has the capabilities of a ‘contributor’ and does NOT (!) have the ability to ‘upload_files’, the code proceeds.

In such a case, the ‘add_action’ function will get activated:

 add_action('admin_init', 'wpturbo_allow_contributor_uploads');

This part is essentially "hooking" the function ‘wpturbo_allow_contributor_uploads’ to the ‘admin_init’ action. The ‘admin_init’ is a hook triggered before any other hook when a user accesses the admin area. This line majorly connects our function ‘wpturbo_allow_contributor_uploads’ to execute when the ‘admin_init’ event happens.

The function ‘wpturbo_allow_contributor_uploads’ is defined next:

function wpturbo_allow_contributor_uploads() {
    $contributor = get_role('contributor');
    $contributor->add_cap('upload_files');
}

Inside the function, it retrieves the contributor role with the ‘get_role’ function and assigns it to the variable ‘$contributor’. The ‘get_role’ function returns the WP_Role object, which includes data about the role capabilities.

Then, it adds the capability of ‘upload_files’ to the contributor role using the ‘add_cap’ method. The ‘add_cap’ function is native to WordPress and it is used to add a capability to a role. Here, ‘upload_files’ is added, which gives the role (in our case, contributor) the ability to upload files.

This will extend the ‘contributor’ capabilities, allowing them to upload files from thereon. Remember, granted permissions will remain in effect until they are explicitly removed, even after the script finishes execution. So, you will only need to run this once.

Register an account to save your snippets or go Pro to get more features.