wordpress上传的文件在哪-WordPress

资源魔 73 0

wordpress上传的文件正在哪?

WordPress默许的上传文件的目次是/wp-content/uploads,并且文件是以年代的方式组织的,尽管咱们能够去掉依照年代组织的选项,或许更改存储门路,但这个设置会使用到全局,不克不及依照特定前提抉择特定目次存储文件。

WordPress上传默许设置

有时分将没有同类型的文件分门别类存储,仿佛连年月目次更无意义。例如幻灯片应该存储正在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' );

将post-type交换成本人的自界说文章类型称号,将你要创立的子目次赋值给$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'];

这段代码来自 http://wordpress.stackexchange.com/questions/35657/how-to-add-more-upload-directories/

为后盾治理页面设定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教程栏目!

以上就是wordpress上传的文件正在哪的具体内容,更多请存眷资源魔其它相干文章!

标签: WordPress wordpress教程 wordpress自学 wordpress技术

抱歉,评论功能暂时关闭!