Skip to content

Instantly share code, notes, and snippets.

@fritids
Forked from chrisguitarguy/upload-dir.php
Created February 16, 2014 21:50
Show Gist options
  • Save fritids/9041141 to your computer and use it in GitHub Desktop.
Save fritids/9041141 to your computer and use it in GitHub Desktop.
<?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