您现在的位置是:首页 > cms教程 > WordPress教程WordPress教程

wordpress上传文件位置在哪

绿春2023-07-01WordPress教程已有人查阅

导读WordPress默认的上传文件的目录是/wp-content/uploads,而且文件是以年月的形式组织的,虽然我们可以去掉按照年月组织的选项,或者更改存储路径

wordpress上传的文件在哪

WordPress默认的上传文件的目录是/wp-content/uploads,而且文件是以年月的形式组织的,虽然我们可以去掉按照年月组织的选项,或者更改存储路径,但这个设置会应用到全局,不能按照特定条件选择特定目录存储文件。

WordPress上传默认设置

有时候将不同类型的文件分门别类存储,似乎比年月目录更有意义。例如幻灯片应该存储在slides目录下,下载文件应该存储在downloads文件夹下。就说幻灯片slideshow,我比较喜欢用自定义文章类型(Custom Post Type)实现,有些幻灯片脚本比较个性,不支持 路径,必须用相对路径,然后用base参数设置相对于哪个文件夹,这样幻灯片必须存储在某个特定的文件夹中,年月形式显然不满足要求。所以,我们需要条件化的设置上传目录。

为Custom Post Type设置上传目录

假设我要将所有在幻灯片类型的文章中上传的文件存储到/wp-content/uploads/slides文件夹中,将下面的代码放到主题的functions.php中即可

function custom_upload_directory( $uploads ) {
    $id = $_REQUEST['post_id'];
        $parent = get_post( $id )->post_parent;
 
    if( "post-type" == get_post_type( $id ) || "post-type" == get_post_type( $parent ) ) {
        $subdir = 'slides';
        $uploads['subdir'] = $subdir;
        $uploads['path'] = $uploads['basedir'].DIRECTORY_SEPARATOR.$subdir;
        $uploads['url'] = $uploads['baseurl'].'/'.$subdir;
    }
    return $uploads;
}
add_filter( 'upload_dir', 'custom_upload_directory' );

将post-type替换成自己的自定义文章类型名称,将你要创建的子目录赋值给$subdir。

将文件保存到插件目录

下面的代码要用在插件中,文件会保存到插件目录下的uploads文件夹下。

/**
 * Change Upload Directory for Custom Post-Type
 *
 * This will change the upload directory for a custom post-type. Attachments will
 * now be uploaded to an "uploads" directory within the folder of your plugin. Make
 * sure you swap out "post-type" in the if-statement with the appropriate value...
 */
function custom_upload_directory( $args ) {
  
    $id = $_REQUEST['post_id'];
    $parent = get_post( $id )->post_parent;
  
    // Check the post-type of the current post
    if( "post-type" == get_post_type( $id ) || "post-type" == get_post_type( $parent ) ) {
        $args['path'] = plugin_dir_path(__FILE__) . "uploads";
        $args['url']  = plugin_dir_url(__FILE__) . "uploads";
        $args['basedir'] = plugin_dir_path(__FILE__) . "uploads";
        $args['baseurl'] = plugin_dir_url(__FILE__) . "uploads";
    }
    return $args;
}
add_filter( 'upload_dir', 'custom_upload_directory' );

如果要以年月形式保存,修改一下代码即可

$args['path'] = plugin_dir_path(__FILE__) . "uploads" . $args['subdir'];
$args['url']  = plugin_dir_url(__FILE__) . "uploads" . $args['subdir'];

这段代码来自 http://wordpress.stackexchange.com/questions/35657/how-to-add-more-upload-directories/

为后台管理页面设定upload_dir

用wp_editor在后台管理页面(比如用add_menu_page创建的页面)创建一个媒体上传功能,希望所有从该页面上传的文件都保存到wp-content/uploads/myfolder目录下。

由 于ajax上传是直接调用wp-admin/async_upload.php文件,只能通过post_id获取post信息,而后台管理页面并非 post,所以判断什么时候应该更改upload_dir有些麻烦。此时,可以用采用判断页面referer的方法,用wp_get_referer() 函数获取引荐url,如果正好与我们的option page url想等,就更该目录。

function custom_upload_directory( $uploads ) { 
   if( wp_get_referer() == 'http://domain.com/wp-admin/admin.php?page=myoptionpage'){
        $subdir = 'myfolder';
        $uploads['subdir'] = $subdir;
        $uploads['path'] = $uploads['basedir'].DIRECTORY_SEPARATOR.$subdir;
        $uploads['url'] = $uploads['baseurl'].'/'.$subdir;
   }
   return $uploads;
}
add_filter( 'upload_dir', 'custom_upload_directory' );

参考信息

filter:upload_dir是在wp_upload_dir()函数中调用的

$upload_dir = wp_upload_dir();
$upload_dir now contains something like the following (if successful)
Array (
    [path] => C:pathtowordpresswp-contentuploads20105
    [url] => http://example.com/wp-content/uploads/2010/05
    [subdir] => /2010/05
    [basedir] => C:pathtowordpresswp-contentuploads
    [baseurl] => http://example.com/wp-content/uploads
    [error] =>
)

本文标签:

很赞哦! ()

相关源码

  • (自适应响应式)工业机床工程农业机械设备网站源码下载框架适用于工程机械、机床设备等工业领域。通过模块调整可快速转型为农业机械、物流设备展示系统。预留7种工业产品展示模板。查看源码
  • (自适应响应式)装修装潢设计公司网站源码下载本模板为装修设计企业打造,采用PbootCMS内核开发,整体设计突出空间美学与功能性结合。首页采用大图轮播展示工程案例,服务项目模块支持三维效果展示,呈现装修设计企业的专业形象与技术实力。查看源码
  • (自适应)英文电子芯片电子元件网站pbootcms模板下载基于PbootCMS内核开发的电子元件类企业专用模板,采用响应式设计技术,适配电子元器件、集成电路、半导体等行业的品牌展示与技术文档发布需求。前端布局针对芯片参数表格与产品规格书展示进行深度优化。查看源码
  • (自适应)行业协会工会机构单位pbootcms网站源码本模板基于PbootCMS内核开发,为行业协会、工会组织及机构单位量身打造。采用响应式布局设计,可自动适配手机、平板等移动设备,数据实时同步更新。模板包含行业资讯查看源码
  • (自适应响应式)HTML5电脑手机电子数码产品配件pbootcms模板下载本模板为电脑配件、手机配件及数码周边产品企业设计,基于PbootCMS内核开发。采用现代化响应式布局,适配各类移动设备,能够专业展示各类电子产品配件参数、应用场景和技术特点。模板内置多种产品展示模块,满足不同类型配件企业的展示需求。查看源码
  • (自适应)绿色农业机械设备农场网站源码下载为现代农业机械领域打造的响应式网站模板,采用PbootCMS内核开发,数据实时同步后台管理。通过简洁大气的视觉设计,有效展示农机产品技术参数与应用场景,帮助客户快速建立专业数字化形象。查看源码
分享笔记 (共有 篇笔记)
验证码: