Black friday Black friday Black friday Cyber monday Cyber monday Cyber monday
Up to 70% off Up to 70% off
Grab Yours

How to allow logged out users to upload attachments?By Milan Jovanovic on 10/10/18 in

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.

Was this answer helpful ? Yes No

Comments are closed.

Wait, Before You Download!

To get your hands on wpDataTables Lite, please enter your email address below. We’ll send you a direct download link and keep you updated on existing features along with helpful tips and tricks!

By continuing, I accept Privacy Policy and T&C
Mail Box