Skip to content

Instantly share code, notes, and snippets.

@mauriciogofas
Forked from rianrietveld/rrwd_upload_dir.php
Last active August 29, 2015 14:15
Show Gist options
  • Save mauriciogofas/558d46870495d402f160 to your computer and use it in GitHub Desktop.
Save mauriciogofas/558d46870495d402f160 to your computer and use it in GitHub Desktop.
Add custom post type name to upload directory WordPress
<?php
/**
* Change Upload Directory for one Custom Post-Type
* Author: https://gist.github.com/RRWD/8705908
* 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', 'rrwd_upload_dir');
$upload = wp_upload_dir();
// remove_filter('upload_dir', 'rrwd_upload_dir');
function rrwd_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;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment