Home > Software design >  How to load local font in php instead of google font with Variable
How to load local font in php instead of google font with Variable

Time:10-12

With the GDPR hitting hard I have to remove google fonts that are loaded per Link from Google Servers.

I have a wordpress theme that has those links everwhere. I am not very good at php, but in this theme I have this link:

  $open_sans_font_url = "https://fonts.googleapis.com/css?family=Open Sans:300italic,400italic,600italic,300,400,600&subset=$subsets&display=fallback";
}

I puts the loaded font into a var. I need to change this so it will load the font from my local font folder.

The problem is, I don't know how to change the link in a way that it will load all the font and font weights/styles. Is it possible? And maybe it is easy but I can't change the whole php file - not enough knowledge from my side and I don't want to risk destroying the theme.

(I know about @font-face and css but not how to add as a link in a var)

I appreciate any help - thank you!

CodePudding user response:

It's not possible to simply replace the variable since it links to a CSS file. There's two options on what you can do. Due to the lack of more code, I can only describe how to do this and not provide direct examples.

Method one: Using your existing CSS, remove the variable

You can add the OpenSans font to your own CSS with the mentioned @font-face, and then remove where the $open_sans_font_url variable is used.

It's likely that this variable is only used to create a <link rel="stylesheet" href="..."> tag to include the Google Font. If you remove this stylesheet inclusion and other possible usages of the variable, you're no longer using Google Fonts.

Method two: Use your own CSS file with the font-face

For this, you need to create your own CSS file, for example opensans.css, with the font-face loaded in. You can then use the Wordpress function get_template_directory_uri() to get the address of your new CSS file within the theme like this:

$open_sans_font_url = get_template_directory_uri() . '/opensans.css';

I hope this helps!

CodePudding user response:

I had the same issue and I solved it by switching to BunnyCDN Fonts, a clone of Google Fonts that (unlike Google Fonts) process requests completely anonymously.

https://fonts.bunny.net

https://css-tricks.com/bunny-fonts/

  • Related