Skip to content

Instantly share code, notes, and snippets.

@irshad-pugMarker
Last active May 19, 2021 06:19
Show Gist options
  • Save irshad-pugMarker/9ab00ae9926ca1e5303080fc65cf048b to your computer and use it in GitHub Desktop.
Save irshad-pugMarker/9ab00ae9926ca1e5303080fc65cf048b to your computer and use it in GitHub Desktop.
Multiple google fonts enqu
/**
 * Google Font URL
 * Combine multiple google font in one URL
 * @link https://shellcreeper.com/?p=1476
 * @author David Chandra <[email protected]>
 */
function tamatebako_google_fonts_url( $fonts, $subsets = array() ){
 
    /* URL */
    $base_url    =  "//fonts.googleapis.com/css";
    $font_args   = array();
    $family      = array();
 
    /* Format Each Font Family in Array */
    foreach( $fonts as $font_name => $font_weight ){
        $font_name = str_replace( ' ', '+', $font_name );
        if( !empty( $font_weight ) ){
            if( is_array( $font_weight ) ){
                $font_weight = implode( ",", $font_weight );
            }
            $family[] = trim( $font_name . ':' . urlencode( trim( $font_weight ) ) );
        }
        else{
            $family[] = trim( $font_name );
        }
    }
 
    /* Only return URL if font family defined. */
    if( !empty( $family ) ){
 
        /* Make Font Family a String */
        $family = implode( "|", $family );
 
        /* Add font family in args */
        $font_args['family'] = $family;
 
        /* Add font subsets in args */
        if( !empty( $subsets ) ){
 
            /* format subsets to string */
            if( is_array( $subsets ) ){
                $subsets = implode( ',', $subsets );
            }
 
            $font_args['subset'] = urlencode( trim( $subsets ) );
        }
 
        return add_query_arg( $font_args, $base_url );
    }
 
    return '';
}


	$google_fonts_url = tamatebako_google_fonts_url(
    array(
        'Open Sans' => array( '400', '600' ),
        'Anton'    => array(),
    ),
    array( 'latin', 'latin-ext' )
);

wp_enqueue_style( 'my-google-fonts', $google_fonts_url  );




#in Footer


function pugmarker_new_scripts_footer() {
    $google_fonts_url = tamatebako_google_fonts_url(
        array(
            'Open Sans' => array( '400', '600' ),
            'Anton'    => array(),
        ),
        array( 'latin', 'latin-ext' )
    );
    
    wp_enqueue_style( 'my-google-fonts', $google_fonts_url  );
}
add_action( 'wp_footer', 'pugmarker_new_scripts_footer' );
function add_rel_preload($html, $handle, $href, $media) {
// if (is_admin())
//     return $html;

if($handle == 'pugmarker-new-style') 
    return $html;

$html = <<<EOT
<link rel='preload' as='style' onload="this.onload=null;this.rel='stylesheet'" 
id='$handle' href='$href' type='text/css' media='all' />
EOT;

return $html;
}

add_filter( 'style_loader_tag', 'add_rel_preload', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment