Site logo

Archived topic

maybe a filter hook in 'generate_get_font_family_css'?

1 reply · Started by Zesen on November 18, 2017

Viewing posts 1–2 of 2

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!

This archived topic is closed to new replies.