Change upload directory for PDF Files 似乎是前进的良好垫脚石。
虽然未经测试,但从代码上看,我的改编将遵循。。。
<?php
    add_filter(\'wp_handle_upload_prefilter\', \'custom_media_library_pre_upload\');
    add_filter(\'wp_handle_upload\', \'custom_media_library_post_upload\');
    function custom_media_library_pre_upload($file){
        add_filter(\'upload_dir\', \'custom_media_library_custom_upload_dir\');
        return $file;
    }
    function custom_media_library_post_upload($fileinfo){
        remove_filter(\'upload_dir\', \'custom_media_library_custom_upload_dir\');
        return $fileinfo;
    }
    function custom_media_library_custom_upload_dir($path){    
        $extension = substr(strrchr($_POST[\'name\'],\'.\'),1);
        if ( !empty( $path[\'error\'] ) ||  $extension != \'zip\' ) 
        {
            return $path; 
        } //error or other filetype; do nothing. 
        elseif ( $extension = \'zip\' )
        {
            $customdir = \'/products\';
        }
        elseif ( $extension = \'jpg\' || $extension = \'jpeg\' || $extension = \'gif\' || $extension = \'png\')
        {
            $customdir = \'/images\';
        }
        $path[\'path\']    = str_replace($path[\'subdir\'], \'\', $path[\'path\']); //remove default subdir (year/month)
        $path[\'url\']     = str_replace($path[\'subdir\'], \'\', $path[\'url\']);      
        $path[\'subdir\']  = $customdir;
        $path[\'path\']   .= $customdir; 
        $path[\'url\']    .= $customdir;  
        return $path;
    }
 这个我会坚持在函数中。php的主题或在相关的主题文件中-不在wordpress核心中,因为更新时会丢失它!
您还可以通过在数组中粘贴匹配项并进行比较来改进我的代码,只是为了简单起见,这样做了。例如。。。
$imagetypes = array( \'jpg\', \'jpeg\', \'gif\', \'png\', \'etc\');
if ( in_array( $extension, $imagetypes ) ) 
{
    $customdir = \'/images\'; 
}