Forked from humayunahmed8/google-fonts-the-right-way.php
Created
November 11, 2022 10:35
-
-
Save milapdave/eea45b0b0b804e54b7c8a64ced0581cc to your computer and use it in GitHub Desktop.
Demonstrates how to properly include Google Fonts into WordPress themes. Ref: http://themeshaper.com/2014/08/13/how-to-add-google-fonts-to-wordpress-themes/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ( ! function_exists( 'prefix_fonts_url' ) ) : | |
/** | |
* Register Google fonts. | |
* | |
* @return string Google fonts URL for the theme. | |
*/ | |
function prefix_fonts_url() { | |
$fonts_url = ''; | |
$fonts = array(); | |
$subsets = ''; | |
/* translators: If there are characters in your language that are not supported by this font, translate this to 'off'. Do not translate into your own language. */ | |
if ( 'off' !== esc_html_x( 'on', 'Karla font: on or off', 'textdomain' ) ) { | |
$fonts[] = 'Karla'; | |
} | |
/* translators: If there are characters in your language that are not supported by this font, translate this to 'off'. Do not translate into your own language. */ | |
if ( 'off' !== esc_html_x( 'on', 'Lato font: on or off', 'textdomain' ) ) { | |
$fonts[] = 'Lato'; | |
} | |
if ( $fonts ) { | |
$fonts_url = add_query_arg( array( | |
'family' => urlencode( implode( '|', $fonts ) ), | |
'subset' => urlencode( $subsets ), | |
), 'https://fonts.googleapis.com/css' ); | |
} | |
return $fonts_url; | |
} | |
endif; | |
/** | |
* Enqueue scripts and styles. | |
*/ | |
function prefix_scripts() { | |
// Add custom fonts, used in the main stylesheet. | |
wp_enqueue_style( 'prefix-fonts', prefix_fonts_url(), array(), null ); | |
} | |
add_action( 'wp_enqueue_scripts', 'prefix_scripts' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment