为WordPress不同日志类型设置不同的上传目录
2014-03-07 16:13
169 查看
WordPress默认的上传文件的目录是/wp-content/uploads,而且文件是以年月的形式组织的,虽然我们可以去掉按照年月组织的选项,或者更改存储路径,但这个设置会应用到全局,不能按照特定条件选择特定目录存储文件。
有时候将不同类型的文件分门别类存储,似乎比年月目录更有意义。例如幻灯片应该存储在slides目录下,下载文件应该存储在downloads文件夹下。就说幻灯片slideshow,我比较喜欢用自定义文章类型(CustomPostType)实现,有些幻灯片脚本比较个性,不支持绝对路径,必须用相对路径,然后用base参数设置相对于哪个文件夹,这样幻灯片必须存储在某个特定的文件夹中,年月形式显然不满足要求。所以,我们需要条件化的设置上传目录。
将post-type替换成自己的自定义文章类型名称,将你要创建的子目录赋值给$subdir。
如果要以年月形式保存,修改一下代码即可
这段代码来自http://wordpress.stackexchange.com/questions/35657/how-to-add-more-upload-directories/
由于ajax上传是直接调用wp-admin/async_upload.php文件,只能通过post_id获取post信息,而后台管理页面并非post,所以判断什么时候应该更改upload_dir有些麻烦。此时,可以用采用判断页面referer的方法,用wp_get_referer()函数获取引荐url,如果正好与我们的optionpageurl想等,就更该目录。
$upload_dir=wp_upload_dir();
$upload_dirnowcontainssomethinglikethefollowing(ifsuccessful)
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]=>
)
有时候将不同类型的文件分门别类存储,似乎比年月目录更有意义。例如幻灯片应该存储在slides目录下,下载文件应该存储在downloads文件夹下。就说幻灯片slideshow,我比较喜欢用自定义文章类型(CustomPostType)实现,有些幻灯片脚本比较个性,不支持绝对路径,必须用相对路径,然后用base参数设置相对于哪个文件夹,这样幻灯片必须存储在某个特定的文件夹中,年月形式显然不满足要求。所以,我们需要条件化的设置上传目录。
为CustomPostType设置上传目录
假设我要将所有在幻灯片类型的文章中上传的文件存储到/wp-content/uploads/slides文件夹中,将下面的代码放到主题的functions.php中即可1 2 3 4 5 6 7 8 9 10 11 12 | functioncustom_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' ); |
将文件保存到插件目录
下面的代码要用在插件中,文件会保存到插件目录下的uploads文件夹下。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** *ChangeUploadDirectoryforCustomPost-Type * *Thiswillchangetheuploaddirectoryforacustompost-type.Attachmentswill *nowbeuploadedtoan"uploads"directorywithinthefolderofyourplugin.Make *sureyouswapout"post-type"intheif-statementwiththeappropriatevalue... */ functioncustom_upload_directory($args){ $id=$_REQUEST[ 'post_id' ]; $parent=get_post($id)->post_parent; //Checkthepost-typeofthecurrentpost 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' ); |
1 2 | $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,如果正好与我们的optionpageurl想等,就更该目录。
1 2 3 4 5 6 7 8 9 10 | 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_dirnowcontainssomethinglikethefollowing(ifsuccessful)
Array(
[path]=>C:pathtowordpresswp-contentuploads20105
[url]=>
[basedir]=>C:pathtowordpresswp-contentuploads
[baseurl]=>
)
相关文章推荐
- ASP.NET MVC在服务端把异步上传的图片裁剪成不同尺寸分别保存,并设置上传目录的尺寸限制
- ASP.NET MVC在服务端把异步上传的图片裁剪成不同尺寸分别保存,并设置上传目录的尺寸限制
- ASP.NET MVC在服务端把异步上传的图片裁剪成不同尺寸分别保存,并设置上传目录的尺寸限制
- 让Rails版的FCKEditor支持动态设置上传目录
- 为什么我上传了flv或MP4文件到服务器,可输入正确地址通过http协议来访问总是出现“无法找到该页”的404错误呢?这就表明mp4格式文件是服务器无法识别的,其实,这是没有在iis中将相应的MIME类型进行设置的原因。那该怎样设置MIME
- squid手动设置日志后squid对该目录无权限问题解决方案
- wordpress防黑教程之保护文件上传目录
- Discuz!教程之如何设置上传的某些类型附件不改为.attach拓展名
- wordpress 上传附件 错误 上级目录没有写权限之解决办法
- 应用键横竖屏切换;label中显示图片;不同类型设备适配的代码;UIWebView字体大小、字体颜色、背景色的设置;
- python在不同层级目录import模块的方法 作者:运维之路 字体:[增加 减小] 类型:转载 时间:2016-01-31 我要评论 这篇文章主要介绍了python 在不同层级目录import
- PHP图片文件上传类型限制扩展名限制大小限制与自动检测目录创建。
- ubuntu server vsftpd 匿名用户上传下载及目录设置
- apache,nginx上传目录无执行权限的设置方法
- 根据需要为不同的类设置不同的输出日志名称及日志格式
- WordPress上传文件类型限制解决办法
- IIS中的上传目录权限设置问题
- Android Studio 配置git目录上传项目到Github并设置忽略文件
- ASP空间设置不同域名到不同目录的方法
- wordpress多站点环境设置上传附件大小