Home / Attachments / Allow ICO files upload
Duplicate Snippet

Embed Snippet on Your Site

Allow ICO files upload

This snippet enables you to upload .ico images to the WordPress media library

<10
Code Preview
php
<?php
add_filter(
	'upload_mimes',
	function ( $mimes ) {
		// By default, only administrator users are allowed to add ICO files.
		// To enable more user types edit or comment the lines below but beware of
		// the security risks if you allow any user to upload ICO files.
		if ( ! current_user_can( 'administrator' ) ) {
			return $mimes;
		}
		$mimes['ico'] = 'image/x-icon';
		return $mimes;
	}
);
add_filter(
	'wp_check_filetype_and_ext',
	function ( $wp_check_filetype_and_ext, $file, $filename, $mimes, $real_mime ) {
		if ( ! $wp_check_filetype_and_ext['type'] ) {
			$check_filetype  = wp_check_filetype( $filename, $mimes );
			$ext             = $check_filetype['ext'];
			$type            = $check_filetype['type'];
			$proper_filename = $filename;
			if ( $type && 0 === strpos( $type, 'image/' ) && 'ico' !== $ext ) {
				$ext  = false;
				$type = false;
			}
			$wp_check_filetype_and_ext = compact( 'ext', 'type', 'proper_filename' );
		}
		return $wp_check_filetype_and_ext;
	},
	10,
	5
);

Comments

Add a Comment