Kirki: A new filter to get specific Google Fonts and their specific Variants loaded

Created on 30 Aug 2016  路  3Comments  路  Source: kirki-framework/kirki

How about adding a filter kirki/enqueue_google_fonts? This would allow us to add specific fonts and their variants in Kirki array of fonts. I'm guessing this is relatively easier than modifying typography field as mentioned in #992.

Here's a scenario in which kirki/enqueue_google_fonts would be a blessing. If we are using a typography field body_typography that is applied to body, then only the selected variant of that font is loaded. But we need to load all common variants (400, italic, 700, 700italic) of the body font because end users use strong and em tags in post content all the time. If those variants are not available the browser forces it to be bold/italic which doesn't look good. If kirki/enqueue_google_fonts is available, then I could add a function that gets font family selected in 'body_typography' and add its common variants (400, italic, 700, 700italic) using kirki/enqueue_google_fonts.

Most helpful comment

Example usage:

Kirki::add_field( 'my_config', array(
  'settings' => 'body_typography',
  'section'  => 'my_section',
  'label'    => esc_html__( 'Typography', 'example' ),
  'type'     => 'typography',
  'default'  => array(
    'font-family'    => 'Open Sans',
    'variant'        => 'regular',
    'subset'         => array( 'latin-ext' ),
    'font-size'      => '14px',
    'letter-spacing' => '0',
    'text-transform' => 'uppercase',
  ),
  'output'   => array(
    array(
      'element'  => '.my-element',
    ),
  ),
) );

in functions.php

function enqueue_body_font_variants( $fonts ) {
  $fonts[ get_theme_mod( 'body_typography', 'Open Sans' ) ] = array(
    'regular',
    'italic',
    '700',
    '700italic',
  );

  return $fonts;
}
add_filter( 'kirki/enqueue_google_fonts', 'enqueue_body_font_variants' );

All 3 comments

Example usage:

Kirki::add_field( 'my_config', array(
  'settings' => 'body_typography',
  'section'  => 'my_section',
  'label'    => esc_html__( 'Typography', 'example' ),
  'type'     => 'typography',
  'default'  => array(
    'font-family'    => 'Open Sans',
    'variant'        => 'regular',
    'subset'         => array( 'latin-ext' ),
    'font-size'      => '14px',
    'letter-spacing' => '0',
    'text-transform' => 'uppercase',
  ),
  'output'   => array(
    array(
      'element'  => '.my-element',
    ),
  ),
) );

in functions.php

function enqueue_body_font_variants( $fonts ) {
  $fonts[ get_theme_mod( 'body_typography', 'Open Sans' ) ] = array(
    'regular',
    'italic',
    '700',
    '700italic',
  );

  return $fonts;
}
add_filter( 'kirki/enqueue_google_fonts', 'enqueue_body_font_variants' );

Great thinking!

Merged the PR so I'll go ahead and close this one. 馃憤 鉂わ笍

Thank you for a prompt merge :+1:

In hindsight, I think what I suggested in #992 is an overkill. Shall I close that issue or do you think it could be useful in any other case?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hussainnayani picture hussainnayani  路  4Comments

ravishakya picture ravishakya  路  7Comments

Radzio1615 picture Radzio1615  路  4Comments

runnickrun picture runnickrun  路  3Comments

2one2 picture 2one2  路  3Comments