WordPress上传文件存放到不同目录的方法
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
有时候将不同类型的文件分门别类存储,似乎比年月目录更有意义。例如幻灯片应该存储在slides目录下,下载文件应该存储在downloads文件夹下。就说幻灯片slideshow,我比较喜欢用自定义文章类型(Custom Post Type)实现,有些幻灯片脚本比较个性,不支持绝对路径,必须用相对路径,然后用base参数设置相对于哪个文件夹,这样幻灯片必须存储在某个特定的文件夹中,年月形式显然不满足要求。所以,我们需要条件化的设置上传目录。
一、为Custom Post Type设置上传目录
假设我要将所有在幻灯片类型的文章中上传的文件存储到/wp-content/uploads/slides文件夹中,将下面的代码放到主题的functions.php中即可
$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'];
三、为后台管理页面设定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想等,就更该目录。
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:\path\to\wordpress\wp-content\uploads\2010\05
[url] => http://example.com/wp-content/uploads/2010/05
[subdir] => /2010/05
[basedir] => C:\path\to\wordpress\wp-content\uploads
[baseurl] => http://example.com/wp-content/uploads
[error] =>
)
相关文章
- 这篇文章主要介绍了wordpress无法安装更新主题插件的解决办法,需要的朋友可以参考下2020-12-27
- 发现几条比较实用的,适合 WordPress 实用的SQL语句。于是就赶紧收集分享出来了,需要的朋友可以参考下2017-09-23
wordpress在安装使用中出现404、403、500及502问题的分析与解决方法
wordpress是很多新手站长搭建个人博客最喜爱的程序,但是最近在使用WordPress的时候遇到了一些问题,所以想着将遇到问题总结分享出来,下面这篇文章主要给大家介绍了关于wo2017-08-11WordPress取消英文标点符号自动替换中文标点符号的优雅方法
这篇文章主要介绍了WordPress取消英文标点符号自动替换中文标点符号的优雅方法,需要的朋友可以参考下2017-04-04- 这篇文章主要给大家介绍了wordpress自定义上传文件类型的方法,如WordPress默认允许上传 .exe 后缀名的可运行文件,那么我们怎么禁止用户在WordPress后台发表文章时上传 .e2016-12-19
- 大家可能发现了当实现了前端用户中心,后台控制面板就失去了作用,那么限制其他用户进入后台控制面板就很有必要了!那么我们要怎么做呢?通过下面这篇文章分享的方法后,只2016-12-19
- 这篇文章主要介绍了WordPress实现回复文章评论后发送邮件通知的功能,涉及wordpress针对评论与邮件的相关操作技巧,需要的朋友可以参考下2016-10-11
- 这篇文章主要介绍了WordPress使用自定义文章类型实现任意模板的方法,可通过自定义文章类型来实现任意模版的使用,具有一定参考借鉴价值,需要的朋友可以参考下2016-10-11
WordPress后台地址被改导致无法登陆后台的简单解决方法
这篇文章主要介绍了WordPress后台地址被改导致无法登陆后台的简单解决方法,简单分析了后台无法登陆的原因与相应的解决方法,涉及针对wordpress配置项的简单修改,需要的朋友2016-10-11- 这篇文章主要介绍了WordPress小工具制作方法,讲述了小工具的制作流程,并附带了近期评论工具制作方法,需要的朋友可以参考下2016-10-10
最新评论