tdiaz

WP Upload Sort By File Type

Sep 17th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.30 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: Upload Sieve
  4.  * Plugin URI: http://a2.gs
  5.   * Description: Place Uploaded Files in directory by type rather than yr/mo.
  6.  * Author: Your Name
  7.  * Author URI: http://a2.gs
  8.  * Version: 0.1.0
  9.  */
  10.  
  11. /**
  12.  * Set custom upload directory
  13.  *
  14.  * Images => /img
  15.  * PDF    => /pdf
  16.  * Misc   => /misc
  17.  *
  18.  * Note: Doesn't work with 'browser uploader'-files will end up in the desirted location with a broken reference in the media library
  19.  * Note: Use with 'wp_update_attachment_metadata' hook if we want to define our own attachment metadata
  20.  * Allowed file types: http://codex.wordpress.org/Uploading_Files#About_Uploading_Files_on_Dashboard
  21.  */
  22. function prefix_custom_upload_directory( $upload ) {
  23.  
  24.     $file_type = array();
  25.  
  26.     // Get file type
  27.     if( ! empty( $_POST['name'] ) ) :
  28.  
  29.         $custom_directory = '';
  30.  
  31.         $file_type = wp_check_filetype( $_POST['name'] );
  32.         $file_ext  = ( $file_type['ext'] ) ? $file_type['ext'] : '';
  33.  
  34.         // set directory based on file type
  35.         if( in_array( strtolower( $file_ext ), array( 'jpg', 'jpeg', 'png', 'gif' ) ) ) :
  36.  
  37.             // Images
  38.             $custom_directory = '/img';
  39.  
  40.         elseif( 'pdf' === strtolower( $file_ext ) ) :
  41.  
  42.             // PDF
  43.             $custom_directory = '/pdf';
  44.  
  45.         else:
  46.  
  47.             // Misc
  48.             $custom_directory = '/misc';
  49.  
  50.         endif;
  51.  
  52.         // update directory
  53.         if( $custom_directory ) :
  54.  
  55.             // remove default subdir (year/month)
  56.             $upload['path']    = str_replace( $upload['subdir'], '', $upload['path'] );
  57.             $upload['url']     = str_replace( $upload['subdir'], '', $upload['url'] );
  58.  
  59.             // update paths
  60.             $upload['subdir']  = $custom_directory;
  61.             $upload['path']   .= $custom_directory;
  62.             $upload['url']    .= $custom_directory;
  63.  
  64.         endif;
  65.  
  66.     endif;
  67.  
  68.     return $upload;
  69.  
  70. }
  71. add_filter( 'wp_handle_upload_prefilter', 'prefix_pre_upload', 0 );
  72. add_filter( 'wp_handle_upload', 'prefix_post_upload', 0 );
  73.  
  74.  
  75.  
  76.  
  77.  
  78. /**
  79.  * Add custom directory filter
  80.  *
  81.  * 'prefix_custom_upload_directory'
  82.  */
  83. function prefix_pre_upload( $file ) {
  84.  
  85.     add_filter( 'upload_dir', 'prefix_custom_upload_directory' );
  86.     return $file;
  87. }
  88.  
  89. /* Place custom code above this line. */
  90. ?>
Advertisement
Add Comment
Please, Sign In to add comment