wordpress 如何修改上传文件的目录
wordpress 默认的上传目录是 /wp-content/uploads 。通过wordpress的钩子可以对这个目录进行修改。比如修改为 网站根目录下的 upload 目录。
wordpress修改上传文件的目录
add_filter('upload_dir', function ($uploads) {
$upload_path = ABSPATH . 'upload';
$upload_url_path = home_url('upload');
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;
}
);
修改前后的对比,其实就是 返回的 $uploads 的值
# 修改前
array(6) {
["path"]=>
string(65) "/web/www.wpcode.cn/webroot/wp-content/uploads/2021/11"
["url"]=>
string(48) "https://www.wpcode.cn/wp-content/uploads/2021/11"
["subdir"]=>
string(8) "/2021/11"
["basedir"]=>
string(57) "/web/www.wpcode.cn/webroot/wp-content/uploads"
["baseurl"]=>
string(40) "https://www.wpcode.cn/wp-content/uploads"
["error"]=>
bool(false)
}
# 修改后
array(6) {
["path"]=>
string(53) "/web/www.wpcode.cn/webroot/upload/2021/11"
["url"]=>
string(36) "https://www.wpcode.cn/upload/2021/11"
["subdir"]=>
string(8) "/2021/11"
["basedir"]=>
string(45) "/web/www.wpcode.cn/webroot/upload"
["baseurl"]=>
string(28) "https://www.wpcode.cn/upload"
["error"]=>
bool(false)
}