allowing visitors to upload pictures to the media libary
-
I am building a custom plugin on a website where visitors are able to submit a picture to the media library. These visitors do not need to be logged in. I am planning on implementing it only one one specific page, however first I want to get it working.
I am granting the visitor role and assigning capibilities in this code snippit
// Add a new user role function custom_plugin_activation() { add_role( 'visitor', __('Visitor'), array( 'upload_files' => true, // Grant 'upload_files' capability 'edit_posts' => true, // Grant 'edit_posts' capability 'publish_posts' => true, // Grant 'publish_posts' capability ) ); } add_action('init', 'custom_plugin_activation'); // Assign the 'visitor' role to non-logged-in users function assign_visitor_role_to_non_logged_in_users() { if (!is_user_logged_in()) { $user = wp_get_current_user(); if (!$user->exists()) { // Assign the 'visitor' role to non-logged-in users $user->add_role('visitor'); } } } add_action('init', 'assign_visitor_role_to_non_logged_in_users'); // Add a filter to restrict allowed file types for the 'Visitor' role function restrict_allowed_file_types_for_visitor($mime_types) { // Allow only PNG files for the 'Visitor' role if (current_user_can('visitor')) { $mime_types = array( 'png' => 'image/png', // Allow only PNG files ); } return $mime_types; } add_filter('upload_mimes', 'restrict_allowed_file_types_for_visitor');
However, I keep getting the following error:
Response text: {“code”:”rest_cannot_create”,”message”:”Je hebt geen toestemming om berichten aan te maken als deze gebruiker.”,”data”:{“status”:401}}
or in english: you do not have the permission to create messages as this user.
Upon visiting the page I’m logging the user role and capabilities to the error log, and they seem to be created.
Am I missing a capability the visitor needs to be able to upload the media?
Or can I allow visitors in another way to add an item to the media library?De pagina waar ik hulp bij nodig heb: [log in om de link te zien]
- Je moet ingelogd zijn om op dit onderwerp te reageren.