返回介绍

attachment_submitbox_metadata()

发布于 2017-09-10 21:31:28 字数 6125 浏览 1151 评论 0 收藏 0

attachment_submitbox_metadata()

Displays non-editable attachment metadata in the publish meta box.


description


源代码

File: wp-admin/includes/media.php

function attachment_submitbox_metadata() {
	$post = get_post();

	$file = get_attached_file( $post->ID );
	$filename = esc_html( wp_basename( $file ) );

	$media_dims = '';
	$meta = wp_get_attachment_metadata( $post->ID );
	if ( isset( $meta['width'], $meta['height'] ) )
		$media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
	/** This filter is documented in wp-admin/includes/media.php */
	$media_dims = apply_filters( 'media_meta', $media_dims, $post );

	$att_url = wp_get_attachment_url( $post->ID );
?>
	<div class="misc-pub-section misc-pub-attachment">
		<label for="attachment_url"><?php _e( 'File URL:' ); ?></label>
		<input type="text" class="widefat urlfield" readonly="readonly" name="attachment_url" id="attachment_url" value="<?php echo esc_attr( $att_url ); ?>" />
	</div>
	<div class="misc-pub-section misc-pub-filename">
		<?php _e( 'File name:' ); ?> <strong><?php echo $filename; ?></strong>
	</div>
	<div class="misc-pub-section misc-pub-filetype">
		<?php _e( 'File type:' ); ?> <strong><?php
			if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) {
				echo esc_html( strtoupper( $matches[1] ) );
				list( $mime_type ) = explode( '/', $post->post_mime_type );
				if ( $mime_type !== 'image' && ! empty( $meta['mime_type'] ) ) {
					if ( $meta['mime_type'] !== "$mime_type/" . strtolower( $matches[1] ) ) {
echo ' (' . $meta['mime_type'] . ')';
					}
				}
			} else {
				echo strtoupper( str_replace( 'image/', '', $post->post_mime_type ) );
			}
		?></strong>
	</div>

	<?php
		$file_size = false;

		if ( isset( $meta['filesize'] ) )
			$file_size = $meta['filesize'];
		elseif ( file_exists( $file ) )
			$file_size = filesize( $file );

		if ( ! empty( $file_size ) ) : ?>
			<div class="misc-pub-section misc-pub-filesize">
				<?php _e( 'File size:' ); ?> <strong><?php echo size_format( $file_size ); ?></strong>
			</div>
			<?php
		endif;

	if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {

		/**
		 * Filters the audio and video metadata fields to be shown in the publish meta box.
		 *
		 * The key for each item in the array should correspond to an attachment
		 * metadata key, and the value should be the desired label.
		 *
		 * @since 3.7.0
		 *
		 * @param array $fields An array of the attachment metadata keys and labels.
		 */
		$fields = apply_filters( 'media_submitbox_misc_sections', array(
			'length_formatted' => __( 'Length:' ),
			'bitrate'          => __( 'Bitrate:' ),
		) );

		foreach ( $fields as $key => $label ) {
			if ( empty( $meta[ $key ] ) ) {
				continue;
			}
	?>
		<div class="misc-pub-section misc-pub-mime-meta misc-pub-<?php echo sanitize_html_class( $key ); ?>">
			<?php echo $label ?> <strong><?php
				switch ( $key ) {
					case 'bitrate' :
echo round( $meta['bitrate'] / 1000 ) . 'kb/s';
if ( ! empty( $meta['bitrate_mode'] ) ) {
echo ' ' . strtoupper( esc_html( $meta['bitrate_mode'] ) );
}
break;
					default:
echo esc_html( $meta[ $key ] );
break;
				}
			?></strong>
		</div>
	<?php
		}

		/**
		 * Filters the audio attachment metadata fields to be shown in the publish meta box.
		 *
		 * The key for each item in the array should correspond to an attachment
		 * metadata key, and the value should be the desired label.
		 *
		 * @since 3.7.0
		 *
		 * @param array $fields An array of the attachment metadata keys and labels.
		 */
		$audio_fields = apply_filters( 'audio_submitbox_misc_sections', array(
			'dataformat' => __( 'Audio Format:' ),
			'codec'      => __( 'Audio Codec:' )
		) );

		foreach ( $audio_fields as $key => $label ) {
			if ( empty( $meta['audio'][ $key ] ) ) {
				continue;
			}
	?>
		<div class="misc-pub-section misc-pub-audio misc-pub-<?php echo sanitize_html_class( $key ); ?>">
			<?php echo $label; ?> <strong><?php echo esc_html( $meta['audio'][$key] ); ?></strong>
		</div>
	<?php
		}

	}

	if ( $media_dims ) : ?>
	<div class="misc-pub-section misc-pub-dimensions">
		<?php _e( 'Dimensions:' ); ?> <strong><?php echo $media_dims; ?></strong>
	</div>
<?php
	endif;
}

更新日志

Versiondescription
3.5.0Introduced.

相关函数

Uses

  • wp-admin/includes/media.php: media_submitbox_misc_sections
  • wp-admin/includes/media.php: audio_submitbox_misc_sections
  • wp-admin/includes/media.php: media_meta
  • wp-includes/l10n.php: _e()
  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: wp_basename()
  • wp-includes/formatting.php: esc_html()
  • wp-includes/formatting.php: esc_attr()
  • wp-includes/formatting.php: sanitize_html_class()
  • wp-includes/functions.php: size_format()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post.php: wp_get_attachment_metadata()
  • wp-includes/post.php: wp_get_attachment_url()
  • wp-includes/post.php: get_post()
  • wp-includes/post.php: get_attached_file()
  • Show 10 more uses Hide more uses

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文