0

I believe as default WordPress won't allow author to upload image via Media button on TinyMCE. It will give you You don't have permission to attach files to this post. error message if you do.

I'm working on a plugin that will allow authors to write and upload images from TinyMCE and looking for solution to grant permission to author to upload images.

How do I achieve this without using other plugins?

Update

I have added below code on my plugin file and it somehow made author to upload image from frontend via Add Media button. However, I'm not quite sure what it really did.

I remember seeing that WP stored role capability data in DB. I'm assuming one of below code save setting for Author and it works after that?

function wh_edit_roles(){
    if ( current_user_can( 'edit_posts' ) ){
        $user = new WP_User(get_current_user_id());
        $user->add_cap('edit_post');
        $user->add_cap('edit_others_pages');
        $user->add_cap('edit_published_pages');
    }
}
add_action('wp_head', 'wh_edit_roles');

1 Answer 1

0

in the basic configuration the user role contributor and subscriber do not have the capability upload_files. The user role author has this capability.

The capability upload_files gives the user the panels "Media" and "Media > Add New".

If you want another role to grant this capability, you can use the function add_cap(). Since such changes are written into the database, it makes sense to use this function at the moment your plugin gets activated in order not to write to the database with every page load (since you could also use this funtion in lets say the init hook). So you should also have a look into register_activation_hook(). Assuming, you place this code into the "root" file of your plugin this should work:

<?php
    function wpse210884_add_upload_files_cap() {
        $role = get_role( 'contributor' ); //The role you want to grant the capability
        $role->add_cap( 'upload_files' );
    }

    register_activation_hook( __FILE__, 'wpse210884_add_upload_files_cap' );
?>
1
  • Thank you for the answer, I forgot to mention that I'm uploading file from front-end with author account. It works fine uploading images on backend with author account but not on frontend form.
    – Ohsik
    Dec 7, 2015 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.