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

有時候將不同類型的文件分門別類存儲,似乎比年月目錄更有意義。例如幻燈片應該存儲在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替換成自己的自定義文章類型名稱,將你要創(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'];
三、為后臺管理頁面設定upload_dir
用wp_editor在后臺管理頁面(比如用add_menu_page創(chuàng)建的頁面)創(chuàng)建一個媒體上傳功能,希望所有從該頁面上傳的文件都保存到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] =>
)
相關文章
CyberPanel安裝WordPress并配置偽靜態(tài)規(guī)則
下面教你如何在 CyberPanel安裝WordPress以及配置偽靜態(tài),需要的朋友可以參考下2023-12-27- 這篇文章主要介紹了wordpress無法安裝更新主題插件的解決辦法,需要的朋友可以參考下2020-12-27
- 發(fā)現幾條比較實用的,適合 WordPress 實用的SQL語句。于是就趕緊收集分享出來了,需要的朋友可以參考下2017-09-23
wordpress在安裝使用中出現404、403、500及502問題的分析與解決方法
wordpress是很多新手站長搭建個人博客最喜愛的程序,但是最近在使用WordPress的時候遇到了一些問題,所以想著將遇到問題總結分享出來,下面這篇文章主要給大家介紹了關于wo2017-08-11WordPress取消英文標點符號自動替換中文標點符號的優(yōu)雅方法
這篇文章主要介紹了WordPress取消英文標點符號自動替換中文標點符號的優(yōu)雅方法,需要的朋友可以參考下2017-04-04- 這篇文章主要給大家介紹了wordpress自定義上傳文件類型的方法,如WordPress默認允許上傳 .exe 后綴名的可運行文件,那么我們怎么禁止用戶在WordPress后臺發(fā)表文章時上傳 .e2016-12-19
- 大家可能發(fā)現了當實現了前端用戶中心,后臺控制面板就失去了作用,那么限制其他用戶進入后臺控制面板就很有必要了!那么我們要怎么做呢?通過下面這篇文章分享的方法后,只2016-12-19
WordPress實現回復文章評論后發(fā)送郵件通知的功能
這篇文章主要介紹了WordPress實現回復文章評論后發(fā)送郵件通知的功能,涉及wordpress針對評論與郵件的相關操作技巧,需要的朋友可以參考下2016-10-11- 這篇文章主要介紹了WordPress使用自定義文章類型實現任意模板的方法,可通過自定義文章類型來實現任意模版的使用,具有一定參考借鑒價值,需要的朋友可以參考下2016-10-11
WordPress后臺地址被改導致無法登陸后臺的簡單解決方法
這篇文章主要介紹了WordPress后臺地址被改導致無法登陸后臺的簡單解決方法,簡單分析了后臺無法登陸的原因與相應的解決方法,涉及針對wordpress配置項的簡單修改,需要的朋友2016-10-11