Friday, August 12, 2022

How to Allow Contributors to Upload Images in WordPress

- Advertisement -spot_img
- Advertisement -Traffic Analysts Tool

Let’s say you have a blog that accepts posts from the general public. You allow people to sign up, write their posts in the WordPress editor, and then send them to you for review. If you think it’s fine, you publish them, otherwise not. Lots of WordPress sites do this. However, in order to prevent people from publishing their posts, you only allow them to sign up as a “Contributor” instead of an author.

Authors can publish posts. Contributors can’t. This is a useful distinction where you want to have control over what appears on your site. It works great, except for one slight problem – contributors can’t upload images!

Why Do You Want Contributors to Upload Images?

These days, articles aren’t complete without pictures. Whether they’re graphs, or screenshots, or some other illustration to make a point, images are crucial for improving the quality of articles. Without images, articles look bleak. So if you’re looking for high-quality posts from contributors, chances are that they will include images.

But if contributors are not allowed to upload images to their posts, then they’ll have to send the pictures to you separately – perhaps by e-mail. And then you’ll have to manually find out where they wanted to insert the images and do it yourself. It’s time-consuming and doesn’t really add any benefit. People who want to write for you will get frustrated as well, knowing they can’t upload images.

- Advertisement -Traffic Analysts Tool

So in this tutorial, I’ll show you how to modify the contributor’s role to allow access to the media library.

Step 1: Checking Disabled Access

In the screenshot below, I have a user called “testuser” with the contributor role as shown here:

Now when I log in as this user and try and open a post for editing, here is what I see:

As you can see, the familiar “Add Media” button is missing. Normally, this allows us to access the media library, upload, and insert posts. What we want, is for this missing media button to become visible to contributors.

Step 2: Adding Code to functions.php

To make this happen, we’re going to add some code to your theme’s function.php. This is a file that contains custom PHP functions specific to each theme. Log in to WordPress as an administrator, go to the dashboard and click Appearance -> Editor:

- Advertisement -Traffic Analysts Tool

On the next page, look to the right-hand side. You’ll see a list of files underneath a drop-down box with your current theme selected. Click the one labeled “Theme Functions” and “functions.php” written underneath it as shown here:

This will open functions.php for editing in the main text area. Scroll all the way down and paste the following at the very bottom:

function allow_contributor_to_upload() {
    $role = get_role( 'contributor' );    
    if ($role->has_cap( 'upload_files')) {
    $role->add_cap( 'upload_files' ); 
add_action( 'admin_init', 'allow_contributor_to_upload');

Here’s a screenshot of what it looks like:

Note: Before you paste this code, make sure you don’t see the following symbol at the bottom of the file:


If you do see it, delete it. It’s a remnant of an old coding standard in WordPress and is dangerous.

After you’ve pasted the code, click the blue button labeled “Update File” to save your changes.

Step 3: Verifying Upload Capability for Contributors

Now let’s see if our code has worked. As before, I open a new post as a contributor. Only this time, I see the following:

As you can see, the button labeled “Add Media” is now visible! Contributors can now upload and add images to their posts like everyone else. But don’t worry! They still can’t publish posts or otherwise harm your site in any way. Now there’s no need for you to manually insert images into contributions!

Optional Step 4: Enable Hotlink Protection for Images from cPanel

There is one danger though. People can upload images to your site and then access them via their URL whenever they want. If someone wants, they can turn your site into a kind of CDN for their images and may consume all the bandwidth allowed by your WordPress hosting provider! I’m not sure why anyone would want to do this, but it’s possible.

So to be on the safe side, just go to your cPanel account and enable hotlinking from the “Security” tab. It’s a good idea to have this enabled anyway to keep your images safe. So now’s as good a time as any to do it!

I hope this tutorial was useful to you and allowed you to streamline your workflow for new post submissions!

About the Author:
Bhagwad has been developing and writing on WordPress for over 8 years. He’s published WordPress plugins, Android apps, and Chrome extensions. He also runs where he compares web hosting offers from providers like SiteGround, and coupons from Bluehost, InMotion, and other major hosting providers.

- Advertisement -
Latest news
- Advertisement -

Liquid Web Storm VPS
Related articles
- Advertisement - Traffic Analysts Tool