Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created January 25, 2013 23:45
Show Gist options
  • Save chrisguitarguy/4638936 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/4638936 to your computer and use it in GitHub Desktop.
Change the upload directory based on post type and file name.
<?php
add_filter('upload_dir', 'cgg_upload_dir');
function cgg_upload_dir($dir)
{
// xxx Lots of $_REQUEST usage in here, not a great idea.
// Are we where we want to be?
if (!isset($_REQUEST['action']) || 'upload-attachment' !== $_REQUEST['action']) {
return $dir;
}
// post types match up?
if (!isset($_REQUEST['post_id']) || 'ml_resource' !== get_post_type($_REQUEST['post_id'])) {
return $dir;
}
$exts = apply_filters('ml_resource_whitelist_ext', array('jpe?g', 'png', 'gif', 'bmp'));
// let images to do their own thing
if (!isset($_REQUEST['name']) || preg_match('/(' . implode('|', $exts) .')$/ui', $_REQUEST['name'])) {
return $dir;
}
// modify the path and url for other files.
$resources = apply_filters('ml_resource_directory', 'resources');
$dir['path'] = path_join($dir['basedir'], $resources);
$dir['url'] = path_join($dir['baseurl'], $resources);
return $dir;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment