按年、月、日组织上传

时间:2012-10-30 作者:PrivateUser

到目前为止,我上传的文件是按年份和月份组织的。但我想把它们按年、按月、按日组织起来。我是说像这样2012/10/30/image goes here

PS:有一个plugin available 这样做。但我不想为这个简单的任务使用插件。谢谢

3 个回复
最合适的回答,由SO网友:brasofilo 整理而成

基于代码的其他Answer of mine 还有这个SO Answer.

它使用post/page/cpt发布日期来构建路径<请注意$the_post->post_date_gmt 也可用。

add_filter(\'wp_handle_upload_prefilter\', \'wpse_70946_handle_upload_prefilter\');
add_filter(\'wp_handle_upload\', \'wpse_70946_handle_upload\');

function wpse_70946_handle_upload_prefilter( $file )
{
    add_filter(\'upload_dir\', \'wpse_70946_custom_upload_dir\');
    return $file;
}

function wpse_70946_handle_upload( $fileinfo )
{
    remove_filter(\'upload_dir\', \'wpse_70946_custom_upload_dir\');
    return $fileinfo;
}

function wpse_70946_custom_upload_dir($path)
{   
    /*
     * Determines if uploading from inside a post/page/cpt - if not, default Upload folder is used
     */
    $use_default_dir = ( isset($_REQUEST[\'post_id\'] ) && $_REQUEST[\'post_id\'] == 0 ) ? true : false; 
    if( !empty( $path[\'error\'] ) || $use_default_dir )
        return $path; // Error: not uploading from a post/page/cpt 

    $the_post = get_post( $_REQUEST[\'post_id\'] );
    $y = date( \'Y\', strtotime( $the_post->post_date ) );
    $m = date( \'m\', strtotime( $the_post->post_date ) );
    $d = date( \'d\', strtotime( $the_post->post_date ) );

    $customdir = \'/\' . $y . \'/\' . $m . \'/\' . $d;

    $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;
}

SO网友:Greenzilla

据我所知,在wp includes\\函数中只调用了一次过滤器“upload\\u dir”。php

我发现这个解决方案要简单得多。它会将所有上传内容放入年/月/日目录中,以确定上传日期。

function upload_dir_filter($uploads){
    $day = date(\'d\');
    $uploads[\'path\'] .= \'/\' . $day;
    $uploads[\'url\']  .= \'/\' . $day;
    return $uploads;
}
add_filter(\'upload_dir\', \'upload_dir_filter\');

SO网友:Westzonesolution Corp

我用这两个代码制作了一个新代码。图像将上载到post date文件夹,而不是上载日期文件夹:

function upload_dir_filter($uploads){

    $the_post = get_post( $_REQUEST[\'post_id\'] );
    $y = date( \'Y\', strtotime( $the_post->post_date ) );
    $m = date( \'m\', strtotime( $the_post->post_date ) );
    $day = date( \'d\', strtotime( $the_post->post_date ) );
    $uploads[\'path\'] .= \'/\' . $day;
    $uploads[\'url\']  .= \'/\' . $day;
    return $uploads;
}
add_filter(\'upload_dir\', \'upload_dir_filter\');

结束

相关推荐

Large Uploads in WordPress

我与一家教会合作,该教会使用WordPress管理其网站和播客。到目前为止,他们每周有200个步骤来发布播客(我夸大了,但这是一个庞大的逐步列表)。除其他外,他们必须:录制布道,掌握布道录音,创建MP3,通过FileZilla将MP3上传到他们的服务器,但是如果可以的话,我想简化步骤4-6。我知道一些在线应用程序能够进行非常大的上传(GMail、Dropbox等)。每个布道都有70-80 MB,太大了,无法用简单的HTML上传表单处理。如果我最大限度地限制PHP文件大小,Flash上传程序是否能够管理这样