Specific upload folder for PDFs in custom Post type in WP multisite

I need to filter uploads to a specific folder for a custom-post type called “document” only for PDFs.

So far, I have:

function custom_upload_directory( $args ) {
$base_directory = '/home/xxx/my_uploadfolder';

$id = $_REQUEST['post_id'];
$parent = get_post( $id )->post_parent;
// Check the post-type of the current post

if( "document" == get_post_type( $id ) || "document" == get_post_type( $parent ) ) {
    $args['path'] = $base_directory;
    $args['url']  = $base_url;
    $args['basedir'] = $base_directory;
    $args['baseurl'] = $base_url;


return $args;
add_filter( 'upload_dir', 'custom_upload_directory' );

It works, but with some problems: : any kind of file is redirected in my_uploadfolder. Also, I can’t delete these files from WP admin once there. Can somebody help?


you might consider using

if(get_post_mime_type($id) == 'application/pdf'){

to check for pdf files.


You might also take a look at the code behind the wp_delete_attachment() function and you can hook into it with the delete attachment action. To remove the files you can use unlink()


Leave a Comment