Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Upload custom post type files in another folder.
<?php
/**
* Upload custom post type files in another folder
* @author Mostafa soufi <mostafa.soufi@hotmail.com>
*/
add_filter( 'upload_dir', function($args) {
if( !isset($_REQUEST['post_id']) ) {
return $args;
}
$post_type_name = get_post_type( $_REQUEST['post_id'] );
if( $post_type_name = 'custom_post_type' ) {
// Set the new path depends on current post_type
$newdir = '/' . $post_type_name;
$args['path'] = str_replace( $args['subdir'], '', $args['path'] ); //remove default subdir
$args['url'] = str_replace( $args['subdir'], '', $args['url'] );
$args['subdir'] = $newdir;
$args['path'] .= $newdir;
$args['url'] .= $newdir;
return $args;
}
return $args;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.