Created
January 4, 2017 11:08
-
-
Save mostafasoufi/c525572d9b4404808b0a7bbf14f09ffa to your computer and use it in GitHub Desktop.
Upload custom post type files in another folder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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