有時候?qū)⒉煌愋偷奈募珠T別類存儲,似乎比年月目錄更有意義。例如幻燈片應(yīng)該存儲在slides目錄下,下載文件應(yīng)該存儲在downloads文件夾下。就說幻燈片slideshow,我比較喜歡用自定義文章類型(Custom Post Type)實現(xiàn),有些幻燈片腳本比較個性,不支持絕對路徑,必須用相對路徑,然后用base參數(shù)設(shè)置相對于哪個文件夾,這樣幻燈片必須存儲在某個特定的文件夾中,年月形式顯然不滿足要求。所以,我們需要條件化的設(shè)置上傳目錄。
一、為Custom Post Type設(shè)置上傳目錄
假設(shè)我要將所有在幻燈片類型的文章中上傳的文件存儲到/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替換成自己的自定義文章類型名稱,將你要創(chuàng)建的子目錄賦值給$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'];
三、為后臺管理頁面設(shè)定upload_dir
用wp_editor在后臺管理頁面(比如用add_menu_page創(chuàng)建的頁面)創(chuàng)建一個媒體上傳功能,希望所有從該頁面上傳的文件都保存到wp-content/uploads/myfolder目錄下。
由 于ajax上傳是直接調(diào)用wp-admin/async_upload.php文件,只能通過post_id獲取post信息,而后臺管理頁面并非 post,所以判斷什么時候應(yīng)該更改upload_dir有些麻煩。此時,可以用采用判斷頁面referer的方法,用wp_get_referer() 函數(shù)獲取引薦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()函數(shù)中調(diào)用的
$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] =>
)