Support

Account

Home Forums General Issues Gallery Upload on Front end form Reply To: Gallery Upload on Front end form

  • I’ve restricted my users with a custom role, granting the ability to manage_photos (their own ones) or manage_others_photos:

    add_filter( 'ajax_query_attachments_args', 'show_only_current_user_attachments', 10 );
    function show_only_current_user_attachments( $query ) {
        $user_id = get_current_user_id();
        if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts') && !current_user_can('manage_others_photos') ) {
            $query['author'] = $user_id;
    		$query['post_parent'] = 0;
        } elseif ($user_id && !current_user_can('activate_plugins')) {
    		$query['post_parent'] = 0;
    	}
        return $query;
    } 

    I also didn’t want to display images that were already attached to other posts, so I’ve filtered those out too in the version above.

    If you want a basic version without my additions:

    add_filter( 'ajax_query_attachments_args', 'show_only_current_user_attachments', 10 );
    function show_only_current_user_attachments( $query ) {
        $user_id = get_current_user_id();
        if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts') ) {
            $query['author'] = $user_id;
    	}
        return $query;
    }