deemi-D-nadeem
deemi-D-nadeem

Reputation: 2514

How to add capability to upload file in wordpress

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.

Upvotes: 1

Views: 1807

Answers (3)

Srikanth S
Srikanth S

Reputation: 1871

I am late to party; I was struggling for a solution. Tried adding capabilities read, edit_posts, upload_files ... to subscriber, contributor roles etc. But it never worked satisfactorily.

Finally, I created a new role and added read, edit_posts, upload_files. It worked like a charm. Code is straight from https://developer.wordpress.org/plugins/users/roles-and-capabilities/

function wporg_simple_role() {
    add_role(
        'simple_role',
        'Simple Role',
        array(
            'read'         => true,
            'edit_posts'   => true,
            'upload_files' => true,
        ),
    );
}
// Add the simple_role.
add_action( 'init', 'wporg_simple_role' );

Hope it helps some one. Trying to tweak existing wordpress role to upload files is a mystery. Go for your own role.

Upvotes: 0

Sufyan Shaik
Sufyan Shaik

Reputation: 23

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

Upvotes: 1

Ashish Patel
Ashish Patel

Reputation: 3614

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

Members

Upvotes: 1

Related Questions