返回介绍

get_allowed_mime_types()

发布于 2017-09-10 22:41:49 字数 3646 浏览 964 评论 0 收藏 0

get_allowed_mime_types( int|WP_User $user = null )

Retrieve list of allowed mime types and file extensions.


description


参数

$user

(int|WP_User) (Optional) User to check. Defaults to current user.

Default value: null


返回值

(array) Array of mime types keyed by the file extension regex corresponding to those types.


源代码

File: wp-includes/functions.php

function get_allowed_mime_types( $user = null ) {
	$t = wp_get_mime_types();

	unset( $t['swf'], $t['exe'] );
	if ( function_exists( 'current_user_can' ) )
		$unfiltered = $user ? user_can( $user, 'unfiltered_html' ) : current_user_can( 'unfiltered_html' );

	if ( empty( $unfiltered ) )
		unset( $t['htm|html'] );

	/**
	 * Filters list of allowed mime types and file extensions.
	 *
	 * @since 2.0.0
	 *
	 * @param array            $t    Mime types keyed by the file extension regex corresponding to
	 *                               those types. 'swf' and 'exe' removed from full list. 'htm|html' also
	 *                               removed depending on '$user' capabilities.
	 * @param int|WP_User|null $user User ID, User object or null if not provided (indicates current user).
	 */
	return apply_filters( 'upload_mimes', $t, $user );
}

更新日志

Versiondescription
2.8.6Introduced.

相关函数

Uses

  • wp-includes/capabilities.php: user_can()
  • wp-includes/capabilities.php: current_user_can()
  • wp-includes/functions.php: wp_get_mime_types()
  • wp-includes/functions.php: upload_mimes
  • wp-includes/plugin.php: apply_filters()

Used By

  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::get_media_types()
  • wp-includes/formatting.php: sanitize_file_name()
  • wp-includes/functions.php: wp_check_filetype()
  • wp-includes/functions.php: wp_check_filetype_and_ext()
  • wp-includes/media.php: wp_plupload_default_settings()
  • wp-includes/media.php: wp_enqueue_media()
  • Show 1 more used by Hide more used by

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

    Function to retrieve the mime type icon of a file by its extension.

    
    <?php
    /**
     * Get mime type icon URL based on file extension.
     *
     * @param $file_ext The file extension to get the icon for.
     * @return string Icon URL.
     */
    function wpdocs_get_icon_by_file_extension($file_ext) {
    	$mimes = get_allowed_mime_types();
    	if ( ! empty( $mimes ) ) {
    		foreach ($ mimes as $type => $mime ) {
    			if ( false !== strpos( $type, $file_ext ) ) {
    				return wp_mime_type_icon($mime);
    			}
    		}
    	}
    }
    ?>
    <img src="<?php echo esc_url( wpdocs_get_icon_by_file_extension( 'mp4' ) ); ?>" />
    

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

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

发布评论

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