Skip to content

Instantly share code, notes, and snippets.

@pippinsplugins
Created May 2, 2012 19:41
Show Gist options
  • Save pippinsplugins/2579659 to your computer and use it in GitHub Desktop.
Save pippinsplugins/2579659 to your computer and use it in GitHub Desktop.
Trying to change upload directory on "download" post type
<?php
function edd_load_upload_filter() {
add_action('load-edit.php', 'edd_change_downloads_upload_dir');
add_action('load-post.php', 'edd_change_downloads_upload_dir');
}
add_action('admin_init', 'edd_load_upload_filter');
function edd_change_downloads_upload_dir() {
$current_screen = get_current_screen();
if($current_screen->post_type !== 'download') { return; }
add_filter('upload_dir', 'edd_set_upload_dir');
}
function edd_set_upload_dir($upload) {
$upload['subdir'] = '/edd' . $upload['subdir'];
$upload['path'] = $upload['basedir'] . $upload['subdir'];
$upload['url'] = $upload['baseurl'] . $upload['subdir'];
return $upload;
}
@pippinsplugins
Copy link
Author

@deckerweb I don't think that would work because the admin_head loads too late.

@pippinsplugins
Copy link
Author

Trying a different approach now:

<?php
function edd_change_downloads_upload_dir() {
    global $pagenow;

    if($pagenow !== 'post.php' && $pagenow !== 'post-new.php') return;

    if($pagenow == 'post.php') {
        $post = get_post($_GET['post']);
        if($post->post_type != 'download') return;
    }   

    add_filter('upload_dir', 'edd_set_upload_dir');
}
add_action('admin_init', 'edd_change_downloads_upload_dir', 999);

function edd_set_upload_dir($upload) {
    $upload['subdir']   = '/edd' . $upload['subdir'];
    $upload['path'] = $upload['basedir'] . $upload['subdir'];
    $upload['url']  = $upload['baseurl'] . $upload['subdir'];
    return $upload;
}

I've confirmed that the basic checks (I know they are no where complete) work, in terms of limiting the filter to when editing a download, but what is really strange is that the upload_dir filter doesn't work, except when I remove the $pagenow checks . . .

@bradyvercher
Copy link

Not sure if this will help, but just in case I didn't misinterpret what you're trying to do, give this a shot:

<?php
function edd_load_upload_filter() {
    global $pagenow;

    if ( ! empty( $_POST['post_id'] ) && ( 'async-upload.php' == $pagenow || 'media-upload.php' == $pagenow ) ) {
        if ( 'download' == get_post_type( $_REQUEST['post_id'] ) ) {
            add_filter( 'upload_dir', 'edd_set_upload_dir' );
        }
    }
}
add_action('admin_init', 'edd_load_upload_filter');

function edd_set_upload_dir($upload) {
    $upload['subdir']   = '/edd' . $upload['subdir'];
    $upload['path'] = $upload['basedir'] . $upload['subdir'];
    $upload['url'] = $upload['baseurl'] . $upload['subdir'];
    return $upload;
}
?>

@pippinsplugins
Copy link
Author

Aha! That appears to work perfectly. I didn't think about the fact that the media uploader has a different $pagenow id . . .that makes a lot more sense now. Props dude!

@thomasgriffin
Copy link

Ahh, I didn't know you were dealing with the media uploader. :-P In that case, check out the Media.php class file in Soliloquy to see how it is done there - it may be helpful for you in your case as well. :-)

@pippinsplugins
Copy link
Author

@thomasgriffin, whoops, yeah, should have mentioned that. The solution Brady suggested above works perfectly.

@thomasgriffin
Copy link

Sweet deal - glad you got it working. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment