-
-
Save rianrietveld/8705908 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', '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; | |
} | |
?> |
Thank you so much for posting this handy snippet! Exactly what I needed. Combined with the Orbisius Media Protector https://orbisius.com/products/wordpress-plugins/orbisius-wp-media-protector I was able to set up a protected folder for internal post type files while keeping the rest of the WP uploads public. Works just fine with files uploaded though custom fields from Toolset WP Types.
Thank you so much for posting this handy snippet! Exactly what I needed. Combined with the Orbisius Media Protector https://orbisius.com/products/wordpress-plugins/orbisius-wp-media-protector I was able to set up a protected folder for internal post type files while keeping the rest of the WP uploads public. Works just fine with files uploaded though custom fields from Toolset WP Types.
Do you know how to change protected folder for Orbisius Media Protector?
It works like a charm!
But it works when thumbnail support of CPT is enabled. Upon removing thumbnail support and uploading media through custom meta field it returns URL www.example.com/file-name/
Any help would be great. Thanks
Thanks for the snippet! Really handy. I just added if(!empty( $_REQUEST["post_id"]) ) { .. } around your code to make it not fail the first time you load WP.