Please wait...

Please check if you maybe have some inline JS minifier tool enabled, e.g. W3TotalCache – inline minification of JS breaks the front-end uploader template logic.

By default WordPress does not allow this. Reason is: logged out user has no ID (id is 0), and no permissions/capabilities enabled for him.

A solution would be to create an actual user that will be used as a single user for all logged-out users, and copy his ID (e.g. id = 100).

Then you can add a simple hook to identify logged out users as User with ID 100. To do this you can use a hook like this:

    function set_loggedout_id( $user_id ){
       if( empty( $user_id ) ){
          $user_id = 100;
       }
       return $user_id;
    }
            
    add_filter( 'determine_current_user', 'set_loggedout_id', 999, 1 );

Basically, it “tells” WordPress to replace the empty ID of a logged out user with a value of 100.
Then you can allow uploading for this user.

By default, WordPress Media Library (used for attachments) allows file uploads only for users who have the upload_files capability. So, everyone who doesn’t have this capability (e.g. a subscriber) will receive an error on trying to upload a file.

To add this capability to subscribers you can e.g. use the User Role Editor plugin. Just enable the upload_files capability for the users which need it and it will work.

Other option is to add this capability dynamically via a hook. To do this you can add e.g. this code to your theme’s functions.php:

            function give_permissions( $allcaps, $cap, $args ) {
                $allcaps['upload_files'] = true;
                return $allcaps;
            }
            add_filter( 'user_has_cap', 'give_permissions', 0, 3 );     

Basically, it enables the upload_files capability to everyone – but you can extend this logic to allow it only for certain users at certain moments.