欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WordPress上傳文件存放到不同目錄的方法

  發(fā)布時(shí)間:2014-03-20 16:09:11   作者:佚名   我要評(píng)論
WordPress默認(rèn)的上傳文件的目錄是/wp-content/uploads,而且文件是以年月的形式組織的,雖然我們可以去掉按照年月組織的選項(xiàng),或者更改存儲(chǔ)路徑,但這個(gè)設(shè)置會(huì)應(yīng)用到全局,不能按照特定條件選擇特定目錄存儲(chǔ)文件

有時(shí)候?qū)⒉煌愋偷奈募珠T別類存儲(chǔ),似乎比年月目錄更有意義。例如幻燈片應(yīng)該存儲(chǔ)在slides目錄下,下載文件應(yīng)該存儲(chǔ)在downloads文件夾下。就說(shuō)幻燈片slideshow,我比較喜歡用自定義文章類型(Custom Post Type)實(shí)現(xiàn),有些幻燈片腳本比較個(gè)性,不支持絕對(duì)路徑,必須用相對(duì)路徑,然后用base參數(shù)設(shè)置相對(duì)于哪個(gè)文件夾,這樣幻燈片必須存儲(chǔ)在某個(gè)特定的文件夾中,年月形式顯然不滿足要求。所以,我們需要條件化的設(shè)置上傳目錄。

一、為Custom Post Type設(shè)置上傳目錄

假設(shè)我要將所有在幻燈片類型的文章中上傳的文件存儲(chǔ)到/wp-content/uploads/slides文件夾中,將下面的代碼放到主題的functions.php中即可

復(fù)制代碼
代碼如下:
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替換成自己的自定義文章類型名稱,將你要?jiǎng)?chuàng)建的子目錄賦值給$subdir。

二、將文件保存到插件目錄

下面的代碼要用在插件中,文件會(huì)保存到插件目錄下的uploads文件夾下。

復(fù)制代碼
代碼如下:

/**
* 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' );

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

復(fù)制代碼
代碼如下:

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

三、為后臺(tái)管理頁(yè)面設(shè)定upload_dir
用wp_editor在后臺(tái)管理頁(yè)面(比如用add_menu_page創(chuàng)建的頁(yè)面)創(chuàng)建一個(gè)媒體上傳功能,希望所有從該頁(yè)面上傳的文件都保存到wp-content/uploads/myfolder目錄下。
由 于ajax上傳是直接調(diào)用wp-admin/async_upload.php文件,只能通過(guò)post_id獲取post信息,而后臺(tái)管理頁(yè)面并非 post,所以判斷什么時(shí)候應(yīng)該更改upload_dir有些麻煩。此時(shí),可以用采用判斷頁(yè)面referer的方法,用wp_get_referer() 函數(shù)獲取引薦url,如果正好與我們的option page url想等,就更該目錄。

復(fù)制代碼
代碼如下:
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()函數(shù)中調(diào)用的

$upload_dir = wp_upload_dir();

復(fù)制代碼
代碼如下:

$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] =>
)

相關(guān)文章

最新評(píng)論