Post Pic

WordPress tip: Allow contributors to upload files

If you have contributors to your blog, you probably got annoyed to see that they can’t upload files and images while writing guest posts for you. Today’s recipe will solve the problem, allowing contributors to upload files to your blog.

Nothng hard with this code: The only thing you have to do is to paste it in your functions.php file:

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

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

Big thanks to Altaf Sayani for his contribution to WpRecipes!

9 Responses

Apr 09 2010 19:09

You might want to fix the HTML entities in the code; if someone copies & pastes that as is, a parse error is in their future. ;)

Apr 09 2010 19:28

@Rick Beckman: Thanks! Just fixed it.

Apr 10 2010 07:27

I think it’s better just using the roles and capabilities plugins.

Apr 11 2010 05:46

I’m actually using the same hack, except I got through all the code that it executes.

During the process, I noticed one important thing – if you remove this snippet, your contributors’ capabilities will remain the same – this function modifies capabilities permanently in the database.

Therefore, to undo, you have to first execute remove_cap() at least once, and then remove the snippet.

Apr 12 2010 23:35

and again – good idea but horrible code – 1-2 SQL queries for every visit of every visitor of any page. Add it e.g. to the login action, if you want to do it this way, but much better is just to add this capability to the contributor role and check it for every existing contributor

Apr 13 2010 08:58

@Tomas

actually, it just proves you don’t know how WordPress internals work. There are no extra SQL queries that run in the if statement. Look up wp_options and “autoload” before making these accusations.

However, regardless of whether there are SQL queries or not, this code is indeed superfluous after you load it one time, as I mentioned before. Might as well just comment it out after the first page run.

Apr 15 2010 09:43

Thanks for the code, just wanted to make downloading files

Jul 13 2010 17:07

how to write functions. I am a total new person and dont have technical background of web programing or designing.

my current funtions.php contains following code. pls guide if i want to add above code in my file
(i am using sail-away theme)

***
”,
‘after_widget’ => ”,
‘before_title’ => ”,
‘after_title’ => ”,
));
?>

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required