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, and I am using Media Uploader to process the upload. 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
andget_current_screen
in mycustom_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.
There must be something identifiable about your form data, such as your input names. Check for one or more of those and process accordingly.
After some investigation, the only things I see that might help are …
POST
data to the callback.However I don’t know this would be set to in you custom plugin page.
$_SERVER['HTTP_REFERER']
appears to be set correctly. You couldparse that to determine if your plugin page is the originating
page.
If I had the full plugin source I could do some testing.