Automatic Dashicons from file type

I’ve been working on using some icons for links in a new theme that I’m building. I wanted to let the client link to any kind of file or web page, and then the theme would detect the file type and apply the appropriate icon. Something like this:

wordpress auto link dashicons
Auto dashicons in action

You can see that the two links have different icons. The first is a link to a PDF document, and the second is a link to a web page.

Here’s the code I used to solve the problem. It could possibly be a bit more elegant! Please suggest any improvements here: https://gist.github.com/keithdevon/db6e90f4a419c4a2c08b

About the author

Co-founder and lead front-end developer at Highrise Digital. Keith has a passion for building beautiful, fast and usable websites.