WordPress自定義上傳路徑 和 生成文件的URL地址

本文目錄1使用代碼恢復2通過代碼直接定義3使用外掛恢復

WordPress 3.5以上的版本,隱藏瞭後臺的媒體(Media)設置頁面 上傳路徑(upload_path)和文件 URL 地址(upload_url_path)的設定,如下圖為之前版本的設置界面:

wpdaxue.com-201212200

通過這裡的設置,你可以自定義文件保存的位置,和生成的地址,這個功能是比較不錯的,就是不知道為啥要隱藏。如果你還是需要自定義,可以試試下面的方法。

使用代碼恢復

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

使用外掛恢復

你可以下載安裝 Ps Display Upload_path For WP3.5 外掛,然後就可以在後臺 – 設置 – 多媒體,看到原來的設置選項啦

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *