WordPress媒體庫顯示文件大小(占用空間)和尺寸

如果你想在後臺 > 媒體庫,直截瞭當地瞭解上傳的文件大小(占用空間)和尺寸,如下圖所示,隻要按照本文的方法進行操作即可。

display-image-size-dimensions-wpdaxue_com

顯示占用空間:下載安裝 Media File Sizes 外掛即可,All Sizes 表示包括縮略圖在內所有大小,Original 表示原圖大小。

顯示尺寸(圖片文件):

將下面的代碼添加到當前主題的functions.php 即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * 在媒體庫顯示文件尺寸
 * From https://www.wpdaxue.com/display-image-size-dimensions.html
 */
add_filter('manage_upload_columns', 'size_column_register');
function size_column_register($columns) {
	$columns['dimensions'] = __('Dimensions');
	return $columns;
}
 
add_action('manage_media_custom_column', 'size_column_display', 10, 2);
function size_column_display($column_name, $post_id) {
	if( 'dimensions' != $column_name || !wp_attachment_is_image($post_id))
		return;
	list($url, $width, $height) = wp_get_attachment_image_src($post_id, 'full');
	echo esc_html("{$width}×{$height}");
}

/**
* 在媒體庫顯示文件尺寸
* From https://www.wpdaxue.com/display-image-size-dimensions.html
*/
add_filter(‘manage_upload_columns’, ‘size_column_register’);
function size_column_register($columns) {
$columns[‘dimensions’] = __(‘Dimensions’);
return $columns;
} add_action(‘manage_media_custom_column’, ‘size_column_display’, 10, 2);
function size_column_display($column_name, $post_id) {
if( ‘dimensions’ != $column_name || !wp_attachment_is_image($post_id))
return;
list($url, $width, $height) = wp_get_attachment_image_src($post_id, ‘full’);
echo esc_html("{$width}×{$height}");
}

如果你想在編輯媒體時,也顯示文件大小,如下圖:

show-file-size-information-on-attachment-screen-wpdaxue_com

隻需將下面的代碼添加到當前主題的 functions.php 即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * 在媒體編輯頁面顯示文件大小
 * From http://maorchasen.com/blog/2013/01/27/show-file-size-information-on-attachment-screen/
 * @author Maor Chasen
 */
function mc_attachment_submitbox_filesize() {
	$post = get_post();
	$filesize = @filesize( get_attached_file( $post->ID ) );
 
	if ( ! empty( $filesize ) && is_numeric( $filesize ) && $filesize > 0 ) : ?>
		<div class="misc-pub-section">
			<?php _e( '文件大小:' ); ?> <strong><?php echo size_format( $filesize ); ?></strong>
		</div>
	<?php
	endif;
}
add_action( 'attachment_submitbox_misc_actions', 'mc_attachment_submitbox_filesize' );

/**
* 在媒體編輯頁面顯示文件大小
* From http://maorchasen.com/blog/2013/01/27/show-file-size-information-on-attachment-screen/
* @author Maor Chasen
*/
function mc_attachment_submitbox_filesize() {
$post = get_post();
$filesize = @filesize( get_attached_file( $post->ID ) );
if ( ! empty( $filesize ) && is_numeric( $filesize ) && $filesize > 0 ) : ?>
<div class="misc-pub-section">
<?php _e( ‘文件大小:’ ); ?> <strong><?php echo size_format( $filesize ); ?></strong>
</div>
<?php
endif;
}
add_action( ‘attachment_submitbox_misc_actions’, ‘mc_attachment_submitbox_filesize’ );

發佈留言