Organising WordPress Media Uploads By File Type

Rather than have them scattered among the myriad date-related folders inside WordPress’ Uploads directory, you might sometimes want (or need) to store files according to their type.

Here’s a set of functions to make that possible.

function slikkr_custom_upload_dir($path) {

	$fileext = substr(strrchr($_POST['name'], '.'), 1);
	$types = array(
		'pdf'   => 'pdf',
		'mp3'   => 'audio'

	if (!empty($path['error']) || ( !array_key_exists($fileext, $types)) ) {
		// These aren't the files you're looking for. He can go about his business.
		return $path;
	$dir = (isset($types[$fileext])) ? $types[$fileext] : NULL;

	$path = array(
		'path'      => WP_CONTENT_DIR . '/uploads/' . $dir,
		'url'       => WP_CONTENT_URL . '/uploads/' . $dir,
		'subdir'    => '',
		'basedir'   => WP_CONTENT_DIR . '/uploads',
		'baseurl'   => WP_CONTENT_URL . '/uploads',
		'error'     => false,

	return $path;

function slikkr_pre_upload($file) {
	add_filter('upload_dir', array($this, 'slikkr_custom_upload_dir') );
	return $file;
function slikkr_post_upload($fileinfo) {
	remove_filter('upload_dir', array($this, 'slikkr_custom_upload_dir') );
	return $fileinfo;

Initialise the filters and you’re done.

add_filter( 'wp_handle_upload_prefilter', array($this, 'slikkr_pre_upload') );
add_filter( 'wp_handle_upload', array($this, 'slikkr_post_upload') );

— tested and working in version 4.6.1 with no 3rd party themes or plugins installed.

About The Author: Sarah

Sarah Cox - Freelance WordPress And Front End Web Developer

Freelance WordPress and frontend web developer since January 1999.

I’m obsessed with Jaws, love dogs, like peach bellinis and black roses and I play with knives. And I bake hobnobs.

Too many pairs of shoes (apparently).

Bit gobby :o)