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

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号