How to remove google font in WordPress for only single page?

I am using WordPress for my site with the Ocean WP Theme and Elementor Page Builder. I can remove Google Fonts from the entire site using below PHP snippet:

add_filter( 'style_loader_src', function($href){
if(strpos($href, "//fonts.googleapis.com/") === false) {
return $href;
}
return false;
});

But I don’t want to remove Google Fonts from the entire site. Instead, I want to remove complete Google Fonts from a single page only, so that I can check and compare page speed with and without Google Fonts.

1 Answer
1

This code filters all style url’s. It returns the url if there is no reference to google fonts. It returns ‘false’ if there is a reference. What you want is to change the condition. It should only return false if there is a reference and you are on a certain page. Logically, this is equivalent to returning the url if there is no reference or you are not on that page. Like this:

if ((strpos($href, "//fonts.googleapis.com/") === false) || !is_page(12345)) {

Where you should replace 12345 with the ID or slug of the page you want to exclude.

Leave a Comment