Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Forked from rianrietveld/rrwd_upload_dir.php
Last active January 24, 2018 11:12
Show Gist options
  • Save joshuadavidnelson/eb86e9d9be9aba9a737a to your computer and use it in GitHub Desktop.
Save joshuadavidnelson/eb86e9d9be9aba9a737a to your computer and use it in GitHub Desktop.
<?php
/**
* Change Upload Directory for one Custom Post-Type
*
* This will change the upload directory for a custom post-type. Attachments for this custom post type will
* now be uploaded to a seperate "uploads" directory. Make
* sure you swap out "post-type" and the "my-dir" with the appropriate values...
* credits to: http://wordpress.stackexchange.com/users/4044/jhdenham
* and http://yoast.com/smarter-upload-handling-wp-plugins/
*/
add_filter('upload_dir', 'custom_upload_dir');
$upload = wp_upload_dir();
function custom_upload_dir( $upload ) {
$id = $_REQUEST['post_id'];
$parent = wp_get_post_parent_id( $id );
$parent_post_type = 'post-type';
// Check the post-type of the current post
if( 'attachment' == get_post_type( $id ) && $parent_post_type == get_post_type( $parent ) )
$upload['subdir'] = '/my-dir' . $upload['subdir'];
$upload['path'] = $upload['basedir'] . $upload['subdir'];
$upload['url'] = $upload['baseurl'] . $upload['subdir'];
return $upload;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment