Latest NewsBlog Detail

Single Post

How to Register custom Google fonts to WordPress themes

How to Register custom Google fonts to WordPress themes

  • WordPress
  • no responses
  • 0 Views
  • Sep 02, 2019
/**
 * Register custom fonts.
 */
function themeslug_fonts_url() {
  $fonts_url = '';
  $fonts     = array();
  $subsets   = 'latin,latin-ext';
 
  /*
   * Translators: If there are characters in your language that are not supported
   * by Oswald, translate this to 'off'. Do not translate into your own language.
   */
  if ( 'off' !== _x( 'on', 'Oswald font: on or off', 'themeslug' ) ) {
    $fonts[] = 'Oswald:400,700';
  }
 
  /*
   * Translators: If there are characters in your language that are not supported
   * by Montserrat, translate this to 'off'. Do not translate into your own language.
   */
  if ( 'off' !== _x( 'on', 'Montserrat font: on or off', 'themeslug' ) ) {
    $fonts[] = 'Montserrat:400,700';
  }
 
 
  if ( $fonts ) {
    $fonts_url = add_query_arg( array(
      'family' => urlencode( implode( '|', $fonts ) ),
      'subset' => urlencode( $subsets ),
    ), 'https://fonts.googleapis.com/css' );
  }
 
 
  return esc_url_raw( $fonts_url );
}

Enqueue scripts and styles

/**
 * Enqueue scripts and styles.
 */
function themeslug_scripts() {
  // Add custom fonts, used in the main stylesheet.
  wp_enqueue_style( 'themeslug-fonts', themeslug_fonts_url(), array(), null );
 
}
add_action( 'wp_enqueue_scripts', 'themeslug_scripts' );

 

Tags
Sarker Allex
Sarker Allex

Co-Founder & Director at Prokash IT

Provesh is Co-Founder and Director at Proveshit. He is responsible for managing, growing open source technologies team and has spearheaded more than 250 projects in Salesforce CRM alone. He provides consulting and advisory to clients looking for services relating to CRM(Customer Relationship Management) and ECM(Enterprise Content Management). In the past, Pratyush has held consulting roles with various global technology leaders, such as Globallogic & HCL in Bangladesh. He holds an Engineering graduate degree from Bangladesh Institute of Technology, Roorkee.