$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Importing Google Fonts in Block Themes — Correct Way|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Importing Google Fonts in Block Themes — Correct Way

matteradmin7PV0评论
@import url("+Sans");
@import url(":100,100i,300,300i,400,400i,700,700i,900,900i");
@import url("+Sans:wght@200;300;400;600;700;800;900&display=swap");

This is an snipped form layout.css which I have commented now as I wanted that the block theme should import such fomts form theme.json. Below is my theme.json —

{
    "$schema": ".json",
    "version": 3,
    "settings": {
        "layout": {
            "contentSize": "700px",
            "wideSize": "1200px"
        },
        "typography": {
            "fontFamilies": [
                {
                    "name": "PT Sans",
                    "slug": "pt-sans",
                    "fontFamily": "\"PT Sans\", sans-serif",
                    "fontFace": [
                        {
                            "fontFamily": "PT Sans",
                            "fontStyle": "normal",
                            "fontWeight": "400",
                            "src": ["+Sans"]
                        }
                    ]
                },
                {
                    "name": "Lato",
                    "slug": "lato",
                    "fontFamily": "\"Lato\", sans-serif",
                    "fontFace": [
                        {
                            "fontFamily": "Lato",
                            "fontStyle": "normal",
                            "fontWeight": "100 900",
                            "src": [":100,100i,300,300i,400,400i,700,700i,900,900i"]
                        }
                    ]
                },
                {
                    "name": "Martel Sans",
                    "slug": "martel-sans",
                    "fontFamily": "\"Martel Sans\", sans-serif",
                    "fontFace": [
                        {
                            "fontFamily": "Martel Sans",
                            "fontStyle": "normal",
                            "fontWeight": "200 300 400 600 700 800 900",
                            "src": ["+Sans:wght@200;300;400;600;700;800;900&display=swap"]
                        }
                    ]
                }
            ]
        }
    },
    "styles": {
        "typography": {
            "fontFamily": "var(--wp--preset--font-family--martel-sans)"
        }
    },
    "templateParts": [
        {
            "name": "sidebar",
            "title": "Sidebar",
            "area": "sidebar"
        }
    ]
}

But the fonts are not importing now.

@import url("https://fonts.googleapis/css?family=PT+Sans");
@import url("https://fonts.googleapis/css?family=Lato:100,100i,300,300i,400,400i,700,700i,900,900i");
@import url("https://fonts.googleapis/css2?family=Martel+Sans:wght@200;300;400;600;700;800;900&display=swap");

This is an snipped form layout.css which I have commented now as I wanted that the block theme should import such fomts form theme.json. Below is my theme.json —

{
    "$schema": "https://schemas.wp/trunk/theme.json",
    "version": 3,
    "settings": {
        "layout": {
            "contentSize": "700px",
            "wideSize": "1200px"
        },
        "typography": {
            "fontFamilies": [
                {
                    "name": "PT Sans",
                    "slug": "pt-sans",
                    "fontFamily": "\"PT Sans\", sans-serif",
                    "fontFace": [
                        {
                            "fontFamily": "PT Sans",
                            "fontStyle": "normal",
                            "fontWeight": "400",
                            "src": ["https://fonts.googleapis/css?family=PT+Sans"]
                        }
                    ]
                },
                {
                    "name": "Lato",
                    "slug": "lato",
                    "fontFamily": "\"Lato\", sans-serif",
                    "fontFace": [
                        {
                            "fontFamily": "Lato",
                            "fontStyle": "normal",
                            "fontWeight": "100 900",
                            "src": ["https://fonts.googleapis/css?family=Lato:100,100i,300,300i,400,400i,700,700i,900,900i"]
                        }
                    ]
                },
                {
                    "name": "Martel Sans",
                    "slug": "martel-sans",
                    "fontFamily": "\"Martel Sans\", sans-serif",
                    "fontFace": [
                        {
                            "fontFamily": "Martel Sans",
                            "fontStyle": "normal",
                            "fontWeight": "200 300 400 600 700 800 900",
                            "src": ["https://fonts.googleapis/css2?family=Martel+Sans:wght@200;300;400;600;700;800;900&display=swap"]
                        }
                    ]
                }
            ]
        }
    },
    "styles": {
        "typography": {
            "fontFamily": "var(--wp--preset--font-family--martel-sans)"
        }
    },
    "templateParts": [
        {
            "name": "sidebar",
            "title": "Sidebar",
            "area": "sidebar"
        }
    ]
}

But the fonts are not importing now.

Share Improve this question asked Apr 27 at 3:39 WordCentWordCent 1,9646 gold badges34 silver badges60 bronze badges 1
  • What does the console say? Any 404 errors under the hood while fetching the fonts? – user3135691 Commented Apr 27 at 22:48
Add a comment  | 

1 Answer 1

Reset to default 3

Make sure the layout.css file is called correctly, both on the frontend and backend.

No need to add fontFace, you have called it via @import :

{
    "$schema": "https://schemas.wp/trunk/theme.json",
    "version": 3,
    "settings": {
        "layout": {
            "contentSize": "700px",
            "wideSize": "1200px"
        },
        "typography": {
            "fontFamilies": [
                {
                    "fontFamily": "\"PT Sans\", sans-serif",
                    "name": "PT Sans",
                    "slug": "pt-sans"
                },
                {

                    "fontFamily": "\"Lato\", sans-serif",
                    "name": "Lato",
                    "slug": "lato"
                },
                {
                    "fontFamily": "\"Martel Sans\", sans-serif",
                    "name": "Martel Sans",
                    "slug": "martel-sans"
                }
            ]
        }
    },
    "styles": {
        "typography": {
            "fontFamily": "var(--wp--preset--font-family--martel-sans)"
        }
    },
    "templateParts": [
        {
            "name": "sidebar",
            "title": "Sidebar",
            "area": "sidebar"
        }
    ]
}

If you mean the font does not appear in the font selection dropdown, it means you need to Reset Style in the block editor in the section: Style >> Typography


Update :

The way to import fonts directly via CSS with @import from Google Fonts or other URLs is not the best way for modern WordPress themes, especially for Full Site Editing (FSE).

There are two ways to add fonts to the Theme:

  1. Place the Font File in the Theme directory, then call it via theme.json with fontFace :

for example adding the same font but different style

your-theme/assets/fonts/nunito/Nunito-VariableFont_wght.woff2

and

your-theme/assets/fonts/nunito/Nunito-Italic-VariableFont_wght.woff2

And call it via theme.json , because the Nunito font has variations of 200 - 1000, so here input the fontWeight according to the font variation, or just what is needed.

{
  "settings": {
    "typography": {
      "fontFamilies": [
        {
          "fontFace": [
            {
              "fontFamily": "Nunito",
              "fontStyle": "normal",
              "fontWeight": "200 1000",
              "src": [
                "file:./assets/fonts/nunito/Nunito-VariableFont_wght.woff2"
              ]
            },
            {
              "fontFamily": "Nunito",
              "fontStyle": "italic",
              "fontWeight": "200 1000",
              "src": [
                "file:./assets/fonts/nunito/Nunito-Italic-VariableFont_wght.woff2"
              ]
            }
          ],
          "fontFamily": "\"Nunito\", sans-serif",
          "name": "Nunito",
          "slug": "nunito"
        }
      ]
    }
  }
}
  1. Using custom functions in PHP

source : webfont-loader

Post a comment

comment list (0)

  1. No comments so far