Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Theme.json font families to Astra custom fonts #65

Open
xoex opened this issue Apr 8, 2023 · 2 comments
Open

Add Theme.json font families to Astra custom fonts #65

xoex opened this issue Apr 8, 2023 · 2 comments

Comments

@xoex
Copy link

xoex commented Apr 8, 2023

Hi. I'm using Astra and Custom fonts plugin. Inside my child theme, I added a theme.json file and added some typographies.
like this :

{
  "version": 2,
  "settings": {
    "typography": {
        {
          "fontFamily": "CustomFont",
          "name": "CustomFont",
          "slug": "custom-font",
          "fontFace": [
            {
              "fontFamily": "CustomFont",
              "fontWeight": "100",
              "fontStyle": "normal",
              "fontStretch": "normal",
              "src": [
                "file:./assets/fonts/somefolder/customfont.woff"
              ]
            },
.....

But astra doesn't automatically detect theme.json font families (at least for me), I wrote this code and i'm using it in child theme,
I think you can add this to astra theme or custom fonts plugin (as a setting, like to check to tickbox and theme.json font families will be added to astra custom fonts).

<?php

class AddThemeJsonFontsToAstra
{
    public static $instance;

    private $fonts;
    public function __construct()
    {
        $this->fonts = [];
        add_action('init', array($this, 'init'));
        add_filter('astra_system_fonts', array($this, 'add_custom_fonts'));

    }

    public function init()
    {
        $fonts = wp_get_global_settings(['typography', 'fontFamilies', 'theme']);
        if (!empty($fonts)) {
            foreach ($fonts as $font) {
                if (isset($font['fontFamily']) && isset($font['fontFace']) && is_array($font['fontFace'])) {
                    $weights = array_column($font['fontFace'], 'fontWeight');
                    $this->fonts[$font['fontFamily']] = ['weights' => $weights];
                }
            }
        }

    }

    public function add_custom_fonts($fonts)
    {
        foreach($this->fonts as $fontFamily => $fontData)
        {
            if (!isset($fonts[$fontFamily])) {
                $fonts[$fontFamily] = $fontData;
            }
        }
        return $fonts;
    }
}

AddThemeJsonFontsToAstra::$instance = new AddThemeJsonFontsToAstra();

Of course code should be optimized, 1. fontfamily should be get from fontFace section, 2. fontWeight can be a range like "200 900". this code can't add them.

@xoex
Copy link
Author

xoex commented Apr 8, 2023

I'll added weight range :

<?php

class AddThemeJsonFontsToAstra
{
    public static $instance;

    private $fonts;
    public function __construct()
    {
        $this->fonts = [];
        add_action('init', array($this, 'init'));
        add_filter('astra_system_fonts', array($this, 'add_custom_fonts'));

    }

    public function init()
    {
        $fonts = wp_get_global_settings(['typography', 'fontFamilies', 'theme']);
        if (!empty($fonts)) {
            foreach ($fonts as $font) {
                if (isset($font['fontFamily']) && isset($font['fontFace']) && is_array($font['fontFace'])) {
                    $weights = array_column($font['fontFace'], 'fontWeight');
                    if (!empty($weights))
                    {
                        if (strpos(trim($weights[0]), ' ') !== false)
                        {
                            $weightRange = explode( ' ', $weights[0]);
                            $start = intdiv(intval($weightRange[0]) , 100);
                            $end = intdiv(intval($weightRange[1]) , 100);
                            $this->fonts[$font['fontFamily']] = ['weights' => array_map('strval', range($start * 100, $end * 100 , 100))];
                        } else {
                            $this->fonts[$font['fontFamily']] = ['weights' => $weights];
                        }
                    }
                }
            }
        }
    }

    public function add_custom_fonts($fonts)
    {
        foreach($this->fonts as $fontFamily => $fontData)
        {
            if (!isset($fonts[$fontFamily])) {
                $fonts[$fontFamily] = $fontData;
            }
        }
        return $fonts;
    }
}

AddThemeJsonFontsToAstra::$instance = new AddThemeJsonFontsToAstra();

@cdils
Copy link

cdils commented Feb 5, 2024

@xoex I realize this is an older ticket, but want to confirm that I am seeing custom fonts loaded in via a child theme's theme.json when using Astra v4.6.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants