get_allowed_mime_types() - WordPress 函数参考中文文档

返回介绍

get_allowed_mime_types()

发布于 2017-09-10 字数3648 浏览 788 评论 0

get_allowed_mime_types( int|WP_User $user = null )

Retrieve list of allowed mime types and file extensions.


描述


参数

$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 );
}

更新日志

Version 描述
2.8.6 Introduced.

相关函数

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' ) ); ?>" />
    

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!