Skip to content

Instantly share code, notes, and snippets.

@maheshwaghmare
Last active January 10, 2018 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maheshwaghmare/49ea3612cf2a6644e18697aa94e56a4f to your computer and use it in GitHub Desktop.
Save maheshwaghmare/49ea3612cf2a6644e18697aa94e56a4f to your computer and use it in GitHub Desktop.
Register/enqueue Google fonts in WordPress Theme.
<?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