有时分将不同类型的文件分门别类存储,仿佛比年月目录更无意义。例如幻灯片应该存储在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' );
二、将文件保留到插件目录
上面的代码要用在插件中,文件会保留到插件目录下的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想等,就更该目录。
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:\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上传文件存放到不同目录的方法》的一些看法。更多内容请查看本栏目更多内容!
当然 Wordpress 2.7 里有“小工具”选项也能管制 Widget ,然而重复试验后发现款式无奈对立,可控...
软件及版本抉择 Ubuntu 14.04 Ubuntu 是目前用户数量首屈一指的发行版,面前有大土豪保护,能够说是轻量级用...
最近忽然发现博客的评论楼层有点成绩,之前不断设置的是“在每个页面顶部显示新的评论”,也就是所谓的倒序...
让媒体库支持pdf分类 这段来自tutsplus的代码能够协助咱们完成如上图所示的成果,将代码放到主题的function...
要害字形容:特点 次要 WordPress 能够 链接 公布 文章 博客 进行 WordPress 的次要特点。WordPress 有很多...
4月更新过文章,文章ID停留在146,5月忙于考试,文章一篇都没写,后果6月前几天写文章的时分,ID从146间接跳...