Remove dashicons.min.css from WordPress

I don’t do a lot code snippets but I figured someone might find this useful.

What’s Are Dashicons

dashicons.min.css is a set of icons mostly used in the WordPress admin. Some themes load them on the front-end is well (or maybe it is default, I really don’t know). However if you are not taking advantage of these icons (such as post type icons) then there is no reason to load them on the front-end of the website.

Why You Might Want to Disable Dashicons

If you’re doing web page speed optimization you know that you should remove unused css and you’ll notice that this css file is about 30KB in size, not huge but really no need to load it if you are not using it, right?

Just the Front-End for Non-Logged In User’s, Please

When I first removed dashicons.min.css by deregistering the style from my functions.php file, I noticed it broke myWordPress admin bar when logged in as an administrator.

Not a huge deal on a site where there is only one administrator but there is a convenience factor. So I used the following in functions.php to load it based on user role. You’ll need to adjust the role capability so it works within your site.

// remove dashicons
function wpdocs_dequeue_dashicon() {
	if (current_user_can( 'update_core' )) {
	    return;
	}
	wp_deregister_style('dashicons');
}
add_action( 'wp_enqueue_scripts', 'wpdocs_dequeue_dashicon' );