Remove Google Fonts Loaded by Themes and Plugins
Dequeues every stylesheet loaded from Google Fonts and removes the related preconnect hints, for speed and GDPR compliance.
Opens WPIDE on
Code
$wpide_drop_google_fonts = function () {
$styles = wp_styles();
foreach ( $styles->registered as $handle => $style ) {
if ( is_string( $style->src ) && false !== strpos( $style->src, 'fonts.googleapis.com' ) ) {
wp_dequeue_style( $handle );
wp_deregister_style( $handle );
}
}
};
add_action( 'wp_enqueue_scripts', $wpide_drop_google_fonts, 999 );
add_action( 'wp_print_styles', $wpide_drop_google_fonts, 1 );
add_filter( 'wp_resource_hints', function ( $urls ) {
return array_values( array_filter( $urls, function ( $url ) {
$href = is_array( $url ) ? ( isset( $url['href'] ) ? $url['href'] : '' ) : $url;
return false === strpos( (string) $href, 'fonts.googleapis.com' ) && false === strpos( (string) $href, 'fonts.gstatic.com' );
} ) );
}, 99 );
Notes
Your theme falls back to system fonts unless you host the fonts yourself, and page builders that inline fonts need their own setting.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.