Remove dashicons.min.css from Front-End for Non-Administrators

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

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.

If your doing web page speed optimization you’ll notice that this guy is about 30KB in size, not huge but really no need to load it if you are not using it, right?

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 but looking at the  front-end of the site. 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' )) {
add_action( 'wp_enqueue_scripts', 'wpdocs_dequeue_dashicon' );