Ondersteuning » Code oplossingen gezocht » 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]

2 reacties aan het bekijken - 1 tot 2 (van in totaal 2)
  • Moderator Jeroen Rotty

    (@jeroenrotty)

    Support Moderator

    Hi!
    These are the Dutch WordPress.org forums, so here we communicate in Dutch. If you don’t speak Dutch, feel free to join the international forums at https://wordpress.org/support/

    Hallo!
    Dit zijn de Nederlandse forums van WordPress.org, dus de voertaal hier is Nederlands. Als je enkel Engels spreekt, dan ben je meer dan welkom op de internationale forums op https://wordpress.org/support.

    Groet,
    Jeroen

    Guido

    (@guido07111975)

    Hoi,

    Voor zover ik weet hoef je hiervoor geen rol of rechten in te stellen. Media-bestanden zijn ook een berichttype (attachment), dus je zou het kunnen proberen met een formulier die na drukken op submit de functie wp_insert_post() aanroept. Met deze functie wordt een bericht (attachment) aangemaakt. Of je gebruikt een plugin zoals WordPress File Upload.

    Guido

2 reacties aan het bekijken - 1 tot 2 (van in totaal 2)