Black Friday sale! Get up to 25% off GP Premium! Learn more ➝

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

Home Forums Support [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 6 years ago by Tom.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • #427286

    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?


    Lead Developer
    Lead Developer

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

    Typically we would just add the font using this filter:

    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.


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