Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error " You dont have permission to attach files to this post" on Wordpress

Settings> MediaWhen non-admin users upload media, They get the following error: enter image description here

Things i have checked:

  1. Wp-content/uploads and all sub folders have permission 755.
  2. Core capabilities and custom for a test user ( who gets this error) is set for yes for media_upload Refer to the image below: Core capabilities

  3. Deactivated all plugins, issue remains same.

  4. To my knowledge, users were able to upload images earlier last week. No change has been done in the code since then.

If anyone has had a similar issue, I'm open for suggestions. Thanks.


UPDATE From wp-admin/includes/ ajax-action.php, I removed the following part:

if ( isset( $_REQUEST['post_id'] ) ) {
    $post_id = $_REQUEST['post_id'];
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        echo wp_json_encode( array(
            'success' => false,
            'data'    => array(
                'message'  => __( "You don't have permission to attach files     to this post." ),
                'filename' => $_FILES['async-upload']['name'],
            )
        ) );

        wp_die();
    }
    }

I realize that this is just sort of a checkpoint to see user capabilities but I dont fully understand why removing this part helped solve the issue. Now test user can upload media successfully ( media upload was successful earlier too) and there is no permission error and "UPLOAD MEDIA" button at the bottom is not greyed any more so I can upload as normal. Thanks

like image 634
shrbisht Avatar asked Aug 07 '15 19:08

shrbisht


2 Answers

Removing core WP code isn't recommended at all!

The cause of this kind of error is often a PHP upload limit in your hosting environment. See here an example of how to change your PHP values : Change the maximum upload file size

But looking at your capabilities screenshot for posts Post Type, it seems your Role doesn't even enable to edit a post. I would first at least enable this Capability : edit_posts. And maybe some other posts-related Capabitilies.

For reference, here is a useful table to help understand Wordress Roles and Capabilities : Capability vs. Role Table

like image 117
webmarka Avatar answered Oct 29 '22 06:10

webmarka


Just Update the wp-admin/includes/ ajax-action.php file, instead of 'edit_post' it should be 'edit_posts'

if ( isset( $_REQUEST['post_id'] ) ) {
$post_id = $_REQUEST['post_id'];
if ( ! current_user_can( 'edit_posts', $post_id ) ) {
    echo wp_json_encode( array(
        'success' => false,
        'data'    => array(
            'message'  => __( "You don't have permission to attach files     to this post." ),
            'filename' => $_FILES['async-upload']['name'],
        )
    ) );

    wp_die();
}
}
like image 38
Shashank Bhagat Avatar answered Oct 29 '22 07:10

Shashank Bhagat