I need to allow users to select fonts from google and have use that without knowing anything about development or wordpress details.
I got most of the solution but missing one critical piece to make it work: where to grab the google fonts from?
I this the way to do this? I guess Webfont loader cannot help here, with the theme.json ?
I have this one example that does work well, but I need to be able to load any google font, where can I get info similar to this below:
fontDefinition(‘Open Sans’, [‘sans-serif’], ‘160 700’, [‘file: https://fonts-static.cdn-one.com/fonts/google/open-sans/open-sans-300.woff’], ‘open-sans’),
];
I tried checking that cdn but I get errors trying to open it.
// making a new font definition to use
function fontDefinition($fontFamily, $fallBacks, $fontWeights, $src, $slug){
return [
'fontFace' => [
[
'fontDisplay' => 'swap',
'fontFamily' => $fontFamily,
'fontStyle' => 'normal',
'fontWeights' => $fontWeights,
'src' => $src
]
],
'fontFamily' => $fontFamily, $fallBacks[0],
'name' => $fontFamily,
'slug' => $slug
];
}
//adapting the theme.json in a filter
function upfast_theme_json_filter($theme_json){
$fontFams=[
fontDefinition('Open Sans', ['sans-serif'], '160 700', ['file: https://fonts-static.cdn-one.com/fonts/google/open-sans/open-sans-300.woff'], 'open-sans'),
];
$new_data = [
'version' => 3,
'settings' => [
'typography' => ['fontFamilies' => $fontFams]
],
'styles' => [
"typography" => [
"fontFamily" => "var(----wp--preset--font-family--open-sans)"
]
]
];
return $theme_json->update_with( $new_data );
}
add_filter('wp_theme_json_data_theme','upfast_theme_json_filter');
user26816420 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.