Created
July 9, 2014 03:24
-
-
Save joelataylor/b3d6977a48c257e544dc to your computer and use it in GitHub Desktop.
WP - change upload dir based on post_type
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
// Save uploads for Products to a products subdir & Artists to an Artist dir | |
function reroute_upload_dir ( $pathdata ) { | |
$post_type = ( isset( $_POST['post_id'] ) ) ? get_post_type( $_POST['post_id'] ) : false; | |
if ( $post_type == 'product' ) { | |
$subdir = '/products'; | |
} elseif ( $post_type == 'artists' ) { | |
$subdir = '/artists'; | |
} elseif ( $post_type === 'post' ) { | |
$subdir = '/blog'; | |
} else { | |
$subdir = ''; | |
} | |
if ( empty( $pathdata['subdir'] ) ) { | |
$pathdata['path'] = $pathdata['path'] . $subdir; | |
$pathdata['url'] = $pathdata['url']. $subdir; | |
$pathdata['subdir'] = $subdir; | |
} else { | |
$new_subdir = $subdir . $pathdata['subdir']; | |
$pathdata['path'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] ); | |
$pathdata['url'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] ); | |
$pathdata['subdir'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['subdir'] ); | |
} | |
return $pathdata; | |
} | |
add_filter( 'upload_dir', 'reroute_upload_dir' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment