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

对待健康,偏见比无知更可怕!微笑不用成本,但能创造财富。赞美不用花钱,但能产生力量。分享不用费用,但能倍增快乐。

WordPress 3.5以上的版本,隐藏了后台的媒体(Media)设置页面 上传路径(upload_path)和文件 URL 地址(upload_url_path)的设定,如下图为之前版本的设置界面:

通过这里的设置,你可以自定义文件保存的位置,和生成的地址,这个功能是比较不错的,就是不知道为啥要隐藏。如果你还是需要自定义,可以试试下面的方法。

使用代码恢复

2013-12-14: @斌果 朋友分享,直接将下面的代码添加到主题的 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');
}

//找回上传设置 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;
}

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; }

注意修改第3、4行中$upload_path 和 $upload_url_path ,例如

1
2
	$upload_path = 'img';
	$upload_url_path = 'https://static.wpdaxue.com/img';

$upload_path = 'img'; $upload_url_path = 'https://static.wpdaxue.com/img';

使用插件恢复

你可以下载安装 Ps Display Upload_path For WP3.5 插件,然后就可以在后台 - 设置 - 多媒体,看到原来的设置选项啦

本文WordPress自定义上传路径 和 生成文件的URL地址到此结束。曾经以为,一次无奈的哭泣,便函是人世间所有的沧桑;一个小小的挫折,便函是人生所有的失败。小编再次感谢大家对我们的支持!

标签: WordPress