返回介绍

wp_mime_type_icon()

发布于 2017-09-11 12:28:05 字数 5686 浏览 1153 评论 0 收藏 0

wp_mime_type_icon( string|int $mime )

Retrieve the icon for a MIME type.


description


参数

$mime

(string|int) (Required) MIME type or attachment ID.


返回值

(string|false) Icon, false otherwise.


源代码

File: wp-includes/post.php

function wp_mime_type_icon( $mime = 0 ) {
	if ( !is_numeric($mime) )
		$icon = wp_cache_get("mime_type_icon_$mime");

	$post_id = 0;
	if ( empty($icon) ) {
		$post_mimes = array();
		if ( is_numeric($mime) ) {
			$mime = (int) $mime;
			if ( $post = get_post( $mime ) ) {
				$post_id = (int) $post->ID;
				$file = get_attached_file( $post_id );
				$ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $file);
				if ( !empty($ext) ) {
					$post_mimes[] = $ext;
					if ( $ext_type = wp_ext2type( $ext ) )
$post_mimes[] = $ext_type;
				}
				$mime = $post->post_mime_type;
			} else {
				$mime = 0;
			}
		} else {
			$post_mimes[] = $mime;
		}

		$icon_files = wp_cache_get('icon_files');

		if ( !is_array($icon_files) ) {
			/**
			 * Filters the icon directory path.
			 *
			 * @since 2.0.0
			 *
			 * @param string $path Icon directory absolute path.
			 */
			$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );

			/**
			 * Filters the icon directory URI.
			 *
			 * @since 2.0.0
			 *
			 * @param string $uri Icon directory URI.
			 */
			$icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) );

			/**
			 * Filters the list of icon directory URIs.
			 *
			 * @since 2.5.0
			 *
			 * @param array $uris List of icon directory URIs.
			 */
			$dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) );
			$icon_files = array();
			while ( $dirs ) {
				$keys = array_keys( $dirs );
				$dir = array_shift( $keys );
				$uri = array_shift($dirs);
				if ( $dh = opendir($dir) ) {
					while ( false !== $file = readdir($dh) ) {
$file = basename($file);
if ( substr($file, 0, 1) == '.' )
continue;
if ( !in_array(strtolower(substr($file, -4)), array('.png', '.gif', '.jpg') ) ) {
if ( is_dir("$dir/$file") )
	$dirs["$dir/$file"] = "$uri/$file";
continue;
}
$icon_files["$dir/$file"] = "$uri/$file";
					}
					closedir($dh);
				}
			}
			wp_cache_add( 'icon_files', $icon_files, 'default', 600 );
		}

		$types = array();
		// Icon basename - extension = MIME wildcard.
		foreach ( $icon_files as $file => $uri )
			$types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file];

		if ( ! empty($mime) ) {
			$post_mimes[] = substr($mime, 0, strpos($mime, '/'));
			$post_mimes[] = substr($mime, strpos($mime, '/') + 1);
			$post_mimes[] = str_replace('/', '_', $mime);
		}

		$matches = wp_match_mime_types(array_keys($types), $post_mimes);
		$matches['default'] = array('default');

		foreach ( $matches as $match => $wilds ) {
			foreach ( $wilds as $wild ) {
				if ( ! isset( $types[ $wild ] ) ) {
					continue;
				}

				$icon = $types[ $wild ];
				if ( ! is_numeric( $mime ) ) {
					wp_cache_add( "mime_type_icon_$mime", $icon );
				}
				break 2;
			}
		}
	}

	/**
	 * Filters the mime type icon.
	 *
	 * @since 2.1.0
	 *
	 * @param string $icon    Path to the mime type icon.
	 * @param string $mime    Mime type.
	 * @param int    $post_id Attachment ID. Will equal 0 if the function passed
	 *                        the mime type.
	 */
	return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id );
}

更新日志

Versiondescription
2.1.0Introduced.

相关函数

Uses

  • wp-includes/cache.php: wp_cache_get()
  • wp-includes/cache.php: wp_cache_add()
  • wp-includes/functions.php: wp_ext2type()
  • wp-includes/link-template.php: includes_url()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post.php: icon_dir
  • wp-includes/post.php: icon_dir_uri
  • wp-includes/post.php: icon_dirs
  • wp-includes/post.php: wp_mime_type_icon
  • wp-includes/post.php: wp_match_mime_types()
  • wp-includes/post.php: get_post()
  • wp-includes/post.php: get_attached_file()
  • Show 7 more uses Hide more uses

Used By

  • wp-includes/customize/class-wp-customize-media-control.php: WP_Customize_Media_Control::to_json()
  • wp-includes/deprecated.php: get_attachment_icon_src()
  • wp-includes/media.php: wp_prepare_attachment_for_js()
  • wp-includes/media.php: wp_playlist_shortcode()
  • wp-includes/media.php: wp_get_attachment_image_src()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Display the icon image of the video format

    
    <?php $img = wp_mime_type_icon( 'video/mp4' ); ?>
    
    
    <img src="<?php echo $img ?>" />
    

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

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

发布评论

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