WordPress自定义上传路径和生成文件的URL地址

By 2018年6月21日 六月 26th, 2018 教程

使用代码恢复

直接将下面的代码添加到主题的 functions.php

1
2
3
4
//找回上传设置
if(get_option('upload_path')=='wp-content/uploads' || get_option('upload_path')==null) {
update_option('upload_path',WP_CONTENT_DIR.'/uploads');
}

通过代码直接定义

将下面的代码添加到主题的 functions.php 的最后一个 ?> 前面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
add_filter( 'upload_dir', 'wpjam_custom_upload_dir' );
function wpjam_custom_upload_dir( $uploads ) {
$upload_path = '';
$upload_url_path = '';

if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) {
$uploads['basedir'] = WP_CONTENT_DIR . '/uploads';
} elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
$uploads['basedir'] = path_join( ABSPATH, $upload_path );
} else {
$uploads['basedir'] = $upload_path;
}

$uploads['path'] = $uploads['basedir'].$uploads['subdir'];

if ( $upload_url_path ) {
$uploads['baseurl'] = $upload_url_path;
$uploads['url'] = $uploads['baseurl'].$uploads['subdir'];
}
return $uploads;
}