Support

Account

Home Forums Backend Issues (wp-admin) upload file in custom folder depend user field

Solved

upload file in custom folder depend user field

  • Hello

    I created a CPT called “custom documentation”.
    In my CPT I have : 1 field “upload” and one “users”.

    I wish when the admin add a file in the upload field, this one is loaded in a specific user custom folder in uploads folder.

    I made this :

    add_filter('acf/upload_prefilter/key=field_61a49554d6a33', 'documents_perso_prefilter',10, 3 );
    
    function documents_perso_prefilter($errors, $file, $field) {
        
        
        if( !current_user_can('edit_pages') ) {
            $errors[] = 'alert message';
        }
    
        add_filter('upload_dir', 'documents_perso');
        
    }
    
    function documents_perso($params) {
        global $post;
        $postid = get_the_ID();
        $users = get_field("users", $postid);  
    if( $users ):
       
       foreach( $users as $user ){
            $username = $user->display_name;
            $custom_dirs = '/uploads/documents/coll/'.$username.'/'; 
            $params['path'] = WP_CONTENT_DIR . $custom_dirs;
            $params['url'] = WP_CONTENT_URL . $custom_dirs;
        
            return $params;
        }
    
    endif;
    
    }

    This is not working, I think it’s because, the param to create the folder or insert the file inside an existing folder is load before the select user ID (to create the correct path).

    There is a way to make it work ?

    thank you

  • A big issue is that you cannot specify multiple locations to upload a file to. ! file can only be uploaded to 1 location. You will only change the upload location to the last user in the loop.

    And yes. The file is uploaded in AJAX and will be uploaded before the value of the ACF field is updated.

    In this case you would need to use an acf/save_post filter, get the users and then duplicate the uploaded image for each user….. I do not know how you would do this duplication.

  • Thank you, I will try another approch 🙂

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.