Register or Enqueue Google fonts

Advertisement

A valid way to register the Google fonts in the theme or plugin.


<?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;

Leave a Reply