Last active
January 10, 2018 07:43
-
-
Save maheshwaghmare/49ea3612cf2a6644e18697aa94e56a4f to your computer and use it in GitHub Desktop.
Register/enqueue Google fonts in WordPress Theme.
This file contains 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
<?php | |
/** | |
* Register/enqueue Google fonts in WordPress Theme | |
* | |
* Note: | |
* 1. Change `theme_slug` with your theme slug. | |
* 2. Change `theme-slug` with your theme slug. | |
* 3. Change `textdomain` with your theme textdomain. | |
*/ | |
if ( ! function_exists( 'theme_slug_scripts' ) ) : | |
/** | |
* Enqueue scripts and styles. | |
*/ | |
function theme_slug_scripts() | |
{ | |
wp_enqueue_style( 'theme-slug-fonts', theme_slug_google_fonts_url(), array(), null ); | |
} | |
add_action( 'wp_enqueue_scripts', 'theme_slug_scripts' ); | |
endif; | |
if ( ! function_exists( 'theme_slug_google_fonts_url' ) ) : | |
/** | |
* Register Google fonts | |
* | |
* @return string Google fonts URL for the theme. | |
*/ | |
function theme_slug_google_fonts_url() { | |
$fonts_url = ''; | |
$fonts = array(); | |
$subsets = 'latin,latin-ext'; | |
/* translators: If there are characters in your language that are not supported by Josefin Slab, translate this to 'off'. Do not translate into your own language. */ | |
if ( 'off' !== _x( 'on', 'Josefin Slab font: on or off', 'textdomain' ) ) { | |
$fonts[] = 'Josefin Slab:400,600,700'; | |
} | |
/* translators: If there are characters in your language that are not supported by Lato, translate this to 'off'. Do not translate into your own language. */ | |
if ( 'off' !== _x( 'on', 'Lato font: on or off', 'textdomain' ) ) { | |
$fonts[] = 'Lato:400,700'; | |
} | |
$query_args = array( | |
'family' => urlencode( implode( '|', $fonts ) ), | |
'subset' => urlencode( $subsets ), | |
); | |
if ( $fonts ) { | |
$fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' ); | |
} | |
return esc_url_raw( $fonts_url ); | |
} | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment