Opened 10 years ago
Last modified 7 weeks ago
#36418 new defect (bug)
Custom MIME type icons lost in WP Media List view
| Reported by: | jhorowitz | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Media | Version: | |
| Severity: | normal | Keywords: | dev-feedback has-patch |
| Cc: | Focuses: | administration |
Description
The ability to define custom MIME icons has been possible since at least v2.5, by hooking into several filters, one of which is icons_dir.
icons_dir allows you to specify additional directory/uri pairs for the wp_mime_type_icon routine to search, ideally adding icon support for additional MIME types.
However, going through SVN history, it seems that almost from the beginning of the availability of that hook, there has been a loophole where the additional icon directories are ignored!
Specifically:
wp_get_attachment_image_src calls wp_mime_type_icon, which in theory could select a custom icon directory added by the icon_dirs hook. It then attempts to fetch width and height information from the selected icon file, but only checks the default wp-includes/images/media directory. Of course, this prevents the custom icons from displaying when fetched through wp_get_attachment_image_src.
Attachments (1)
Change History (9)
This ticket was mentioned in Slack in #core-media by desrosj. View the logs.
9 years ago
#4
@
5 years ago
This bug still exists if you try to load custom icon images by filtering wp_mime_type_icon. They work fine in Grid view of the Media Library, but if you view as List, it ignores the custom path and tries to find them in the core media directory, throws PHP errors, and no image is loaded at all.
Here's an example I tried in functions.php of a custom theme. Works in Grid view, fails in List view.
<?php add_filter( 'wp_mime_type_icon', function( $icon, $mime, $post_id ) { if( 'text/plain' === $mime && $post_id > 0 ) $icon = get_template_directory_uri() . '/library/images/icons/txt.png'; if( 'application/pdf' === $mime && $post_id > 0 ) $icon = get_template_directory_uri() . '/library/images/icons/pdf.png'; return $icon; }, 10, 3 );
#6
@
7 weeks ago
Solved: Based on the example code from @liquidRock, I'm using the code in my plugin that's similar to the following:
<?php add_filter( 'wp_mime_type_icon', function( $icon, $mime, $post_id ) { if( 'application/gpx+xml' === $mime && $post_id > 0 ) { $icon = 'https://my-domain.tld/path/to/my-icons-dir/gpx-icon.svg'; } return $icon; }, 10, 3 );
<?php add_filter( 'icon_dir', function ( $path ) { return '/path/to/my-icons-dir/'; }, 10, 1 );
And you must copy the ./wp-includes/images/media/default.svg to /path/to/my-icons-dir/.
This ticket was mentioned in PR #12014 on WordPress/wordpress-develop by @jhorowitz.
7 weeks ago
#7
- Keywords has-patch added
This ensures wp_get_attachment_image_src checks the same directories as wp_mime_type_icon; simply a refresher of the patch in the Trac ticket.
Trac ticket: https://core.trac.wordpress.org/ticket/36418
## Use of AI Tools
AI assistance: No
Tool(s): n/a
Model(s): n/a
Used for: n/a
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Patch to make wp_get_attachment_image_src check the same icon file directories as wp_mime_type_icon