How preconnect Google fonts from Google CDN to WordPress sit... open

How preconnect Google fonts from Google CDN to WordPress site

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

Simply including google fonts in your head.php file is a mistake
To connect Google fonts, it is correct to use the WordPress function for connecting style files wp_enqueque_style

Find our Google fonts, or get their code on https://fonts.google.com/

For example, connect fonts:

<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Oswald:wght@400;500;600;700&family=Rubik&display=swap" rel="stylesheet">

Let’s write a function that will create a fonts link. Add the code below to function.php

<?php 

function create_urls() {
    $fonts_url = '';
    $families = array();
    $families[] = 'Oswald:wght@400;500;600;700';
    $families[] = 'Rubik';

    $query_args = array(
        'family' => urlencode(implode('|', $families)),
    );
    $fonts_url = add_query_arg($query_args, 'https://fonts.googleapis.com/css');
    return esc_url_raw($fonts_url);

}

?>

Load our create_urls() function having added in function.php code

<?php 

wp_enqueue_style('mytheme-fonts', create_urls(), array(), '1.0'  );

?>

If you use Redux it has a special field where you can select text and it will be automatically loaded.

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation