Forked from blainerobison/gist:e802658da007e6e806b1
Last active
August 24, 2022 00:49
-
-
Save chrisdavidmiles/b8f1309b5cc9ec9eabc94f1b0007d153 to your computer and use it in GitHub Desktop.
WordPress Plugin: Custom Uploads Directory By File 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
<?php | |
/** | |
* Plugin Name: Custom Uploads Directory By File Type | |
* Plugin URI: https://gist.github.com/chrisdavidmiles/a7f3fbf09616b4c445a45310c0f88e05 | |
* Description: This plugin automatically sorts uploads into a folder corresponding to their file type. Images go into <code>/img</code>, documents go into <code>/docs</code>, video goes into <code>/img</code>, and the rest goes into <code>/misc</code>. Note: This code doesn't work with 'browser uploader'. | |
* Author: Forked from Blaine Robison | |
* Author URI: https://gist.github.com/blainerobison/e802658da007e6e806b1 | |
* Version: 0.2 | |
*/ | |
/** | |
* Set custom upload directory | |
* | |
* Images => /img | |
* Docs => /docs | |
* Video => /video | |
* Misc => /misc | |
* | |
* Note: Doesn't work with 'browser uploader' | |
* Note: Use with 'wp_update_attachment_metadata' hook if we want to define our own attachment metadata | |
* Allowed file types: http://codex.wordpress.org/Uploading_Files#About_Uploading_Files_on_Dashboard | |
* This code was forked from https://gist.github.com/blainerobison/e802658da007e6e806b1 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) die; | |
function prefix_custom_upload_directory( $upload ) { | |
$thesiteurl = site_url(); | |
$file_type = array(); | |
// Get file type | |
if( ! empty( $_POST['name'] ) ) : | |
$custom_directory = ''; | |
$file_type = wp_check_filetype( $_POST['name'] ); | |
$file_ext = ( $file_type['ext'] ) ? $file_type['ext'] : ''; | |
// set directory for images | |
if( in_array( strtolower( $file_ext ), array( 'jpg', 'jpeg', 'png', 'gif' ) ) ) : | |
$custom_directory = '/img'; | |
// set directory for documents | |
elseif( in_array( strtolower( $file_ext ), array( 'pdf', 'doc', 'docx', 'rtf', 'txt', 'odt', 'pages', 'dox' ) ) ) : | |
$custom_directory = '/docs'; | |
// set directory for videos | |
elseif( in_array( strtolower( $file_ext ), array( 'webm', 'mkv', 'flv', 'vob', 'ogv', 'ogg', 'avi', 'mov', 'qt', 'wmv', 'yuv', 'rm', 'rmvb', 'asf', 'amv', 'mp4', 'm4p', 'm4v', 'mpg', 'mp2', 'mpeg', 'mpe', 'mpv', 'm2v', 'svi', '3gp', '3g2', 'mxf', 'roq', 'nsv', 'f4v', 'f4p', 'f4a', 'f4b' ) ) ) : | |
$custom_directory = '/video'; | |
// everything else, miscellaneous | |
else: | |
$custom_directory = '/misc'; | |
endif; | |
// update directory | |
if( $custom_directory ) : | |
// remove default subdir (year/month) | |
$upload['path'] = str_replace( $upload['subdir'], '', $upload['path'] ); | |
$upload['url'] = str_replace( $upload['subdir'], '', $upload['url'] ); | |
// update paths | |
$upload['subdir'] = $custom_directory; | |
$upload['path'] .= $custom_directory; | |
$upload['url'] .= $custom_directory; | |
endif; | |
endif; | |
return $upload; | |
} | |
// Add custom directory filter 'prefix_custom_upload_directory' | |
function prefix_pre_upload( $file ) { | |
add_filter( 'upload_dir', 'prefix_custom_upload_directory' ); | |
return $file; | |
} | |
// Remove custom directory filter 'prefix_custom_upload_directory' | |
function prefix_post_upload( $fileinfo ) { | |
remove_filter( 'upload_dir', 'prefix_custom_upload_directory' ); | |
return $fileinfo; | |
} | |
// Use the filters | |
add_filter( 'wp_handle_upload_prefilter', 'prefix_pre_upload', 0 ); | |
add_filter( 'wp_handle_upload', 'prefix_post_upload', 0 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment