Support

Account

Home Forums General Issues Force an Image/File upload to a particular directory Reply To: Force an Image/File upload to a particular directory

  • Not sure I know how to use that.

    Do you fire it out of a first function like you did with the other pair of them, in the other thread? (per that thread, I’m already using upload_prefilter to change the upload directory for this Image field; I also want to rename the file).

    I modified my working code but it doesn’t work for the renaming…

    add_filter('acf/upload_prefilter/key=field_6140a6da30a17', 'prefilter_avatar_upload');
    function prefilter_avatar_upload($errors) {
        // This one added, but doesn't work, unsure what to do
        add_filter('wp_handle_upload_prefilter', 'avatar_upload_rename' );
        // in this filter we add a WP filter that alters the upload path
        add_filter('upload_dir', 'modify_avatar_upload_dir');
        return $errors;
    }
    
        // (Old) second filter
        function modify_avatar_upload_dir($uploads_avatars) {
            // here is where we later the path
            $uploads_avatars['path'] = $uploads_avatars['basedir'].'/users/avatars';
            $uploads_avatars['url'] = $uploads_avatars['baseurl'].'/users/avatars';
            // $uploads_avatars['subdir'] = 'avatars';
            return $uploads_avatars;
        }
    
        // + Rename the file for saving
        function avatar_upload_rename( $file ) {
            $file['name'] = 'test-prefix-' . $file['name'];
            return $file;
        }