WordPress默认附件上传路径是wp-content/uploads
,我们可以修改为别的目录。对于用子域名来统一管理附件的来说,还是很实用的。
当然只限于在同空间服务区内的目录,如果是外网的话,这么改是没有用的。
修改方法:
打开主题下的functions.php
文件,添加如下代码:
/**
* 自定义文件上传地址到其他路径(同服务器内)
* $upload_path 定义上传的路径
* $upload_url_path 定义显示的链接网址
*/
add_filter( 'upload_dir', 'custom_upload_dir' );
function custom_upload_dir( $uploads ) {
$upload_path = '/images';
$upload_url_path = 'http://images.xxxx.com';
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;
}
这样通过wordpress后台上传的附件会直接上传到 images 目录里
附件链接地址也会显示为 http://images.xxxx.com/
恢复默认上传路径:
如果使用的主题将上传路径修改为其他的,又不知道在哪儿改,只需要添加以下代码到主题的functions.php
就可以将路径恢复到默认的wp-content/uploads
:
if(get_option('upload_path')=='wp-content/uploads' || get_option('upload_path')==null) {
update_option('upload_path',WP_CONTENT_DIR.'/uploads');
}
引用通告: wordpress恢复后台上传路径设置 | ZZ'Blog