• I have developed a plugin for my business requirement which uploads files to the server programmatically.

    The problem I am facing is that each document may be uploaded to different location, lets say the location is specified on the fly from somewhere ( I am using api so I get the location and file in the API POST request).

    upload_dir filter helps in changing upload_dir if it is already known, however, in my case, I won’t know in advance the location to upload files.

    Is there some way I can pass destination path to the upload_dir filter which is only changed for my plugin and not globally?

    something like following?

    public function _change_upload_directory($dirs, $location)
        {
            $dirs['subdir'] = $location;
            $dirs['path'] = $dirs['basedir'] . $location;
            $dirs['url'] = $dirs['baseurl'] . $location;
            return $dirs;
        }
        // somewhere in the code I pass the destination to upload document
        add_filter( 'upload_dir', [ $this, '_change_upload_directory'], 'directory1/directory2');

The topic ‘specify custom directory/path to upload_dir filter’ is closed to new replies.