Abdur Rehman
Abdur Rehman

Reputation: 39

Wordpress custom role can't upload images using media on front end

I have created a custom template with plugin that allows user (Role: Customer) to upload pictures from front end but when I try to upload a picture it says "You don't have permission to attach files to this post."

I can upload pictures through /wp-admin > Media - Add New but not from the front end.

Here is my code;

`function CA_enqueue_scripts() {
wp_enqueue_media();
wp_enqueue_script(
        'some-script', get_template_directory_uri() . 
'/custom/assets/js/edit_profile.js', array('jquery'), '2017-09-27'
);
}

/**
* This filter insures users only see their own media
*/
function CA_filter_media($query) {
   // admins get to see everything
   if (!current_user_can('manage_options'))
       $query['author'] = get_current_user_id();
    return $query;
}

add_action('wp_enqueue_scripts', 'CA_enqueue_scripts');
add_filter('ajax_query_attachments_args', 'CA_filter_media');`

JS:

(function($) {

$(document).ready( function() {
var file_frame;
$( '#upload_image' ).on( 'click', function( event ) {
    event.preventDefault();

    if ( file_frame ) {
        file_frame.open();
        return;
    }

    file_frame = wp.media.frames.file_frame = wp.media({
        title: $( this ).data( 'uploader_title' ),
        button: {
            text: $( this ).data( 'uploader_button_text' ),
        },
        multiple: false // set this to true for multiple file selection
    });

    file_frame.on( 'select', function() {
        attachment = file_frame.state().get('selection').first().toJSON();
        console.log(attachment);
    });

    file_frame.open();
    });
   });

})(jQuery);

I have set granted 'upload, edit post' permissions to role using using User Role Editor plugin.

Thanks!

Upvotes: 0

Views: 1457

Answers (1)

Tin Chu
Tin Chu

Reputation: 1

I resolved this issues installing https://wordpress.org/plugins/user-role-editor/, then I realized that my CPT, the one that I created to be modified by this Role(Customer) was with capabilities of page. I modified on the plugin option..

Hope that helps you.

Upvotes: 0

Related Questions