• Resolved Jamie

    (@rabillion)


    Hey there,
    i have two questions. How to allow guests/visitors to add images to the media libary? I’ved tried to set a role and set the cap to allow and also tried with this code snippet, but it’s not working for me. I’m working OOP

    My second question is how to restrict to selected only images files? I cant’t set the attribute accept image/* because the cmb file type is an hidden input field <input type="hidden" accept="image/*" value="0" id="kradblatt_image_id" name="kradblatt_image_id" class="cmb2-upload-file-id"><div class="cmb2-media-status" id="kradblatt_image_id-status">

    https://www.ads-software.com/plugins/cmb2/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    What errors, if any are you seeing with the attempts to allow the uploads?

    Since we stick to WP-based functions for handling the uploading, the WP filters for limiting file types should still apply.

    Thread Starter Jamie

    (@rabillion)

    An error occurred in the upload. Please try again later.

    only registered users can upload images

    Thread Starter Jamie

    (@rabillion)

    my code:

    public function __construct() {
            add_action( 'init', array( $this, 'add_new_role' ) );
            add_action( 'init', array( $this, 'set_guest_role' ) );
        }
    public function set_guest_role() {
            $user = wp_get_current_user();
            if ( empty( $user->user_login ) ) {
                $user->data->user_login = 'guest';
                $user->caps             = 'guest';
                $user->roles[0]         = 'guest';
            }
        }
    
        public function add_new_role() {
    
            add_role( 'guest', __( 'Guest' ), array(
                    'level_1'                  => true,
                    'upload_files'             => true,
                    'manage_media_library'     => true,
                    'read'                     => false,
                    'create_posts'             => false,
                    'edit_posts'               => false,
                    'edit_pages'               => false,
                    'edit_others_posts'        => false,
                    'manage_categories'        => false,
                    'publish_posts'            => false,
                    'edit_themes'              => false,
                    'install_plugins'          => false,
                    'update_plugin'            => false,
                    'update_core'              => false
    
                )
            );
        }

    And how can i restrict users to see only the current uploaded file?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    This is for non-logged in people right? If yes, not sure they’re ever getting that role, but I could be wrong.

    Can you provide all your metabox code as well?

    Thread Starter Jamie

    (@rabillion)

    That’s right. Only for not logged people

    /**
         * Initiate our hooks.
         */
        public static function init() {
            add_action( 'cmb2_init', array( __CLASS__, '_register_person_formfields_metabox' ) );
        }
    
        /**
         * person field
         * Hook in and add a metabox to select templates
         */
        public function _register_person_formfields_metabox() {
    
            new_cmb2_box( array(
                'id'           => self::$prefix . 'personform',
                'title'        => __( 'Person', 'cmb2' ),
                'object_types' => array( 'person_posts', ), // Post type
                'classes'      => 'person-form',
                'show_names'   => true,
                'cmb_styles'   => false,
                'fields'       => array(
                    array(
                        'name'       => __( 'Title', 'cmb2' ),
                        'id'         => self::$prefix . 'title',
                        'type'       => 'text',
                        'attributes' => array(
                            'required' => 'required',
                        ),
                    ),
                    array(
                        'name' => __( 'Email', 'cmb2' ),
                        'id'   => self::$prefix . 'email',
                        'type' => 'text_email',
                    ),
                    array(
                        'name' => __( 'Phone', 'cmb2' ),
                        'id'   => self::$prefix . 'phone',
                        'type' => 'text',
                    ),
                    array(
                        'name'       => __( 'Zip', 'cmb2' ),
                        'id'         => self::$prefix . 'zipcode',
                        'type'       => 'text',
                        'attributes' => array(
                            'type' => 'number',
                        ),
                    ),
                    array(
                        'name'         => __( 'Photo', 'cmb2' ),
                        'id'           => self::$prefix . 'image',
                        'type'         => 'file',
                        'preview_size' => array( 100, 100 ),
                        'options'      => array(
                            'url' => false,
                        ),
                        'attributes'   => array(
                            'accept' => 'image/*', // not working
                        )
                    ),
                    array(
                        'name'       => __( 'Description', 'cmb2' ),
                        'id'         => self::$prefix . 'content',
                        'type'       => 'textarea',
                        'options'    => array(
                            'textarea_rows' => 12,
                        ),
                        'attributes' => array(
                            'required' => 'required',
                        ),
                    ),
                ),
            ) );
        }
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    What about the code for the frontend display? Using it all to recreate in my own local site for testing and debugging.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to allow guest to upload images from frontend?’ is closed to new replies.