Black Friday Sale! Get $20 off GP Premium, $40 off our new Lifetime license, and 45% off license renewals/extensions! Learn more

[Support request] maybe a filter hook in 'generate_get_font_family_css'?

Home Forums Support maybe a filter hook in 'generate_get_font_family_css'?

  • This topic has 1 reply, 2 voices, and was last updated 3 years ago by Tom.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #427286
    Zesen

    Hey Tom, got a relatively complicated situation here.

    We are using GP for Asian users and therefore we need to support Asian fonts. For the rest of this article, I would assume we are trying to support Chinese fonts.

    The standards when writing CSS rules for Asian fonts is that we add the font name right before sans-serif/serif. For example: font-family: Arial, 黑体, sans-serif;. In this case, if a paragraph has both English and Chinese in it, the English characters would use Arial and Chinese characters would fall back to use 黑体.

    Now I am writing a custom plugin to add more customizer settings and crontrols to allow users to select Chinese fonts, which has already been done. The problem is to use these settings in the frontend. Now, what I am doing is to hack the core:

    
    // in typography/functions/functions.php, locate generate_get_font_family_css function, right before 'return $output;', add:
    if ( isset( $generate_settings[ 'cn_windows_' . $font ] ) && '' !== trim( $generate_settings[ 'cn_windows_' . $font ] ) ) {
    	$cn_font = $generate_settings[ 'cn_windows_' . $font ];
    	if ( preg_match( '/sans-serif$/', $output, $match ) )
    		$output = preg_replace( '/sans-serif$/', $cn_font . ', sans-serif', $output );
    	else if ( preg_match( '/serif$/', $output, $match ) )
    		$output = preg_replace( '/serif$/', $cn_font . ', serif', $output );
    	else if ( '' !== trim( $output ) )
    		$output =  $output . ', ' . $cn_font;
    }
    

    By doing this, I could make use of both the English fonts provided by GP, as well as the Chinese fonts added by my plugin. However, I don’t think this is the way to go though since it would be cleared everytime GP gets updated.

    Is there a better way to achieve the same result here? Maybe a filter hook of $output? Maybe other solutions?

    Thanks!

    GP Premium 1.5.3
    #427610
    Tom
    Lead Developer
    Lead Developer

    Yea, I think filtering the $output here would be best.

    Typically we would just add the font using this filter: https://docs.generatepress.com/article/generate_typography_default_fonts/

    However that only works for single fonts – it doesn’t work when adding a comma separated list.

    I’ll look into adding this filter in the next version.

    Thanks!

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.