自定义 wordpress 上传目录

/

自定义 wordpress 上传目录

wordpress 的上传目录默认为 wp-content/uploads,怎样自定义上传目录呢?下面的代码展示了在任何情况下,包括wordpress 地址和站点地址不同的情况下,将所有文件上传至站点根目录下的 uploads 文件夹。

在主题的 functions.php 中添加如下内容:

function custom_upload_dir($uploads) {
	$subdir = '';
	if ( get_option( 'uploads_use_yearmonth_folders' ) ) {
		// Generate the yearly and monthly dirs
		if ( !$time ) $time = current_time( 'mysql' );
		$y = substr( $time, 0, 4 );
		$m = substr( $time, 5, 2 );
		$subdir = "/$y/$m";
	}
	$uploads['basedir'] = $_SERVER['DOCUMENT_ROOT'] . '/uploads';
	$uploads['baseurl'] = 'http://'. $_SERVER['SERVER_NAME'] . '/uploads';
	$uploads['path'] = $uploads['basedir'] . $subdir;
	$uploads['url'] = $uploads['baseurl'] . $subdir;
	$uploads['error'] = false;
	return $uploads;
}
add_filter('upload_dir', 'custom_upload_dir');

Comments