Skip to content

Instantly share code, notes, and snippets.

@mostafasoufi
Created January 4, 2017 11:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mostafasoufi/c525572d9b4404808b0a7bbf14f09ffa to your computer and use it in GitHub Desktop.
Save mostafasoufi/c525572d9b4404808b0a7bbf14f09ffa to your computer and use it in GitHub Desktop.
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