yasseen
9/26/2014 - 4:23 PM

custom_upload_dir.php

<?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();
// remove_filter('upload_dir', 'customupload_dir');

function custom_upload_dir( $upload ) {

$id = $_REQUEST['post_id'];
  $parent = get_post( $id )->post_parent;

  // Check the post-type of the current post
  if( "post-type" == get_post_type( $id ) || "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;
}
?>