Tuesday, 28 May 2013

Changing upload directory for plugin uploads only

Changing upload directory for plugin uploads only

I've spent a lot of time reading the other threads on this question, but I just cannot get my code to function properly.
I'm building a plugin, and I've set up an upload form on my custom admin page. Please see below for simplified code:
class myPlugin() {
  function __construct(){
    add_filter('wp_handle_upload_prefilter', array( $this, 'handle_upload_prefilter') );
    add_filter('wp_handle_upload', array( $this, 'handle_upload') );
  }

  function handle_upload_prefilter( $file ) {
    add_filter('upload_dir', array( $this, 'custom_upload_dir' ) );
    return $file;
  }

  function handle_upload( $fileinfo )   {
    remove_filter('upload_dir', array( $this, 'custom_upload_dir' ) );
    return $fileinfo;
  }

  function custom_upload_dir($args) {  
    $args['path'] = $args['basedir'] . "/mypath" . $args['subdir'];
    $args['url'] = $args['baseurl'] . "/mypath" . $args['subdir'];
    return $args;
  }
}
The code above works, and it changes the upload directory to the path that I specify. However, it is changing the upload directory for ALL uploads. I'd like my code to run only when uploading from my custom admin page.
This is what I've tried so far (and has not worked for me):
I've tried using conditionals to test for $pagenow and get_current_screen in my custom_upload_dir function, but that seems to fail every time and uses the default upload paths.
I've tried testing for conditional in my __construct but I get returned an error (I guess it's too early in the WP lifecycle).
Tried running the conditional in my handle_upload_prefilter function also to no avail.

No comments:

Post a Comment