WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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):

  1. 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.
  2. 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).
  3. Tried running the conditional in my handle_upload_prefilter function also to no avail.
share|improve this question
up vote 3 down vote accepted

There must be something identifiable about your form data, such as your input names. Check for one or more of those and process accordingly.

function custom_upload_dir($args) {   
    if (isset($_POST['something'])) {
        $args['path'] = $args['basedir'] . "/mypath" . $args['subdir'];
        $args['url'] = $args['baseurl'] . "/mypath" . $args['subdir'];
    return $args;

After some investigation, the only things I see that might help are ...

  1. The post ID is passed through the POST data to the callback. However I don't know this would be set to in you custom plugin page.
  2. $_SERVER['HTTP_REFERER'] appears to be set correctly. You could parse that to determine if your plugin page is the originating page.

If I had the full plugin source I could do some testing.

share|improve this answer
Wouldn't this only work once the form has been submitted? Perhaps I should have clarified in my original question that I'm using the Media Uploader (I'll go ahead and edit my post to include this information). Unless my understanding of the process is incorrect, since I won't have actually submitted my form, I can't check to see if an input is set, correct? – ahnkee May 28 '13 at 19:01
Also, in my custom_upload_dir function, I tried to test for $_GET['page'] == 'my_page_name' but that did not work either – ahnkee May 28 '13 at 19:07
I'll see what I can do. – s_ha_dum May 28 '13 at 20:17
thanks so much for your help! – ahnkee May 28 '13 at 20:18
@s_ha_dum with your $_SERVER['HTTP_REFERER'] suggestion i have a workable solution. Only thing i had to hardcopy the =="urlfrommyplugin" – alex May 31 '13 at 8:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.