ulfben

Custom Upload Dir

Mar 23rd, 2019
127
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //at init:
  2. add_filter('wp_handle_upload_prefilter', 'cud_pre_upload');
  3.  
  4. // Called once for each uploaded file.
  5. function cud_pre_upload($file){
  6.     [...]
  7.     add_filter('upload_dir', 'cud_custom_upload_dir');
  8.     return $file;
  9. }
  10.  
  11. function cud_custom_upload_dir($path){     
  12.     if(!empty($path['error'])) { return $path; } //error; do nothing.  
  13.     $customdir = cud_generate_path();
  14.     $path['path']    = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month)
  15.     $path['url']     = str_replace($path['subdir'], '', $path['url']);     
  16.     $path['subdir']  = $customdir;
  17.     $path['path']   .= $customdir;
  18.     $path['url']    .= $customdir; 
  19.     return $path;
  20. }
  21.  
  22. function cud_generate_path(){  
  23.     global $post, $post_id, $current_user, $cud_file_ext, $cud_file_type, $cud_rpc_id; 
  24.     $post_id = (!empty($post_id) ? $post_id : (!empty($_REQUEST['post_id']) ? $_REQUEST['post_id'] : (!empty($cud_rpc_id) ? $cud_rpc_id : '')));
  25.     $my_post;
  26.     if(empty($post) || (!empty($post) && is_numeric($post_id) && $post_id != $post->ID)){
  27.         $my_post = get_post($post_id);
  28.     }  
  29.     $customdir = = get_option('custom_upload_dir_template');
  30.    
  31.     //defaults
  32.     $user_id = $post_type = $post_name = $author = '';
  33.     $time = (!empty($_SERVER['REQUEST_TIME'])) ? $_SERVER['REQUEST_TIME'] : (time() + (get_option('gmt_offset')*3600));
  34.     $user_id = (is_user_logged_in() && !empty($current_user)) ? $current_user->ID : '';    
  35.     if(empty($user_id)){   
  36.         $current_user = wp_get_current_user();
  37.         if($current_user instanceof WP_User){
  38.             $user_id = $current_user->ID;
  39.         }
  40.     }
  41.     if(!empty($my_post)){      
  42.         $post_id = $my_post->ID;
  43.         $time = ($my_post->post_date == '0000-00-00 00:00:00') ? $time : strtotime($my_post->post_date);   
  44.         $post_type = $my_post->post_type;
  45.         $author = $my_post->post_author;
  46.         $post_name = (!empty($my_post->post_name)) ? $my_post->post_name : (!empty($my_post->post_title) ? sanitize_title($my_post->post_title) : $post_id);   
  47.     }else{     
  48.         $post_id = '';
  49.     }  
  50.    
  51.     $date = explode(" ", date('Y m d H i s', $time));          
  52.     $tags = array('%post_id%','%postname%','%post_type%','%year%','%monthnum%','%month%', '%day%','%hour%','%minute%','%second%', '%file_ext%', '%file_type%');
  53.     $replace = array($post_id, $post_name, $post_type, $date[0], $date[1], $date[1], $date[2], $date[3], $date[4], $date[5], $cud_file_ext, $cud_file_type);
  54.    
  55.     $customdir = str_replace($tags, $replace, $customdir); //do all cheap replacements in one go.
  56.     $customdir = str_replace('%permalink%',     cud_get_permalink($post_id),$customdir);
  57.     $customdir = str_replace('%author%',        cud_get_user_name($author), $customdir);
  58.     $customdir = str_replace('%author_role%',   cud_get_user_role($author), $customdir);
  59.     $customdir = str_replace('%current_user%',  cud_get_user_name($user_id),$customdir);
  60.     $customdir = str_replace('%parent_name%',   cud_get_parent_slug($my_post),  $customdir);   
  61.     $customdir = str_replace('%post_category%', cud_get_category_name($post_id),    $customdir);/*DEPRECATED SINCE 3.0.3*/
  62.     $customdir = str_replace('%post_categories%',cud_get_categories_name($post_id), $customdir);/*DEPRECATED SINCE 3.0.3*/
  63.                
  64.     //Now, let's deal with taxonomies (category, tags, custom) 
  65.     $matches = array();
  66.     if(preg_match_all('/%(.*?)%/s', $customdir, $matches) == true){
  67.         for($i = 0; $i < count($matches[0]); $i++){        
  68.             if(taxonomy_exists($matches[1][$i])){
  69.                 $customdir = str_replace($matches[0][$i], cud_get_taxonomies($post_id, $matches[1][$i]), $customdir);
  70.             }else{
  71.                 $customdir = str_replace($matches[0][$i], '', $customdir);
  72.             }
  73.         }
  74.     }  
  75.     $customdir = cud_leadingslashit($customdir); //for good measure.
  76.     $customdir = untrailingslashit($customdir);
  77.     while(strpos($customdir, '//') !== false){
  78.         $customdir = str_replace('//', '/', $customdir); //avoid duplicate slashes.
  79.     }  
  80.     return apply_filters('cud_generate_path', $customdir, $post_id);
  81. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×