How to add capability to upload file in wordpress

1.5k views Asked by At

I am using paid theme of WordPress. The theme is about car dealing. In this theme there is a Role => Dealer.

When I login as a dealer I can edit my profile (But no Image Option). My client want an upload field for dealer's company logo. I create a media up loader and its working but not perfectly. When I click on upload logo button, the media up loader popup, then I select an image, Now the media up loader start processing and some mini seconds it shows me this error:

enter image description here

I search about roles in code and I found this code in parent theme:

add_role('tdp_dealer', 'Vehicle Dealer', array(
    'read' => true, // True allows that capability
    'edit_dealer_fields' => true
));

Then I search for Upload Capability and found upload_files. I write this cap in the code but its not working.

add_role('tdp_dealer', 'Vehicle Dealer', array(
    'read' => true, // True allows that capability
    'edit_dealer_fields' => true,
    'upload_files' => true,
    'edit_posts' => true
));

Then I also try this code but its also not working:

function tdp_add_dealer_caps() {
    // gets the author role
    $role = get_role( 'tdp_dealer' );

    // This only works, because it accesses the class instance.
    // would allow the author to edit others' posts for current theme only
    //$role->add_cap( 'edit_dealer_fields', true );
    $role->add_cap( 'upload_files', true );
}
add_action( 'init', 'tdp_add_dealer_caps');

So guys guide me that how can I upload files, images as a dealer user. Hope you understand my question.

2

There are 2 answers

0
Ashish Patel On BEST ANSWER

Try word press plugin in which you can assign permission fr uploading to custom user role:

Members

0
Sufyan Shaik On

You should add_action to admin_init. It worked for me.

add_action('admin_init', 'allow_tdp_dealer_uploads');
function allow_tdp_dealer_uploads() {
    $tdp_dealer = get_role('tdp_dealer');
    $tdp_dealer->add_cap('upload_files');
}

Add this to plugin's main file i.e index.php