欢迎光临
我们一直在努力

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

建站超值云服务器,限时71元/月

本文目录
[隐藏]

  • 1使用代码恢复
  • 2通过代码直接定义
  • 3使用插件恢复

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

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

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

使用代码恢复

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://img.wpdaxue.com';

$upload_path = ‘img’; $upload_url_path = ‘https://img.wpdaxue.com’;

使用插件恢复

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » WordPress自定义上传路径 和 生成文件的URL地址
分享到: 更多 (0)