Site logo

Archived topic

how to show custom fonts in block editor gutenberg?

13 replies · Started by Axel on September 25, 2020

Viewing posts 1–14 of 14

hi there,

being aware this must have been has been asked before, but there seems no way to get it working ... already tried several ways.

setup:
custom local font (incl. variants) added to website in dedicated folder of child theme, enqueued via css and functions.php.
working like a charme on frontend and within customizer / gp.
so i would assume everything is set up right :)

so as we are planning to use gutenberg for editing blog posts it would be quite nice if the local fonts would show up there too - sadly they dont. always getting fallback 'times new roman'.

tried several ways to get it done via snipplets i found here (and others there) - nothing seems to work.

is there something that has been changed according to that recently and theres probably a 'new' way to do so?
(btw. ... if wp can pick up these fonts in customizer, why not in gutenberg??? weird!)

any hint highly appreciated and thanks for that in advance.

Hi there,

You need to load the @font-face CSS in the block editor. Is the CSS currently in your child theme stylesheet? If so, we can load that in the block editor, but it will bring in your other CSS as well.

Alternatively, you can just add the font-face stuff into the editor:

add_action( 'enqueue_block_editor_assets', function() {
    wp_enqueue_style( 'your-handle', 'URL TO YOUR FILE' );
} );

hi tom,

currently css is in child, yes - fonts also, within dedicated 'fonts' folder.
loading this complete css may not be that great, as there are many other things to be contained.

so i assume "just add the font-face stuff" into the editor means, to set up a separate css file just for that?
a second question for the understanding of the dumb of us - the 'your-handle'-part should be picked up from a specific area or may it be just whatever?

tried that separate stuff before without success :/ but worth another try anyways.

thanks for your help.

Yea, that's what I would do. Create a new .css file with the @font-face definitions, and make sure the URLs to the font files are correct. Then add the URL to that new CSS file to the function I provided above.

your-handle can be anything you like as long as it's unique, it's just an identifier that WordPress uses for enqueued files.

... finally works.

strangely it does not work with relative url within function... weird.

thanks.

That's strange - relative URLs should work if they're inside a CSS file and the fonts are relative to that file.

Glad you got it working :)

... relative url in css is o.k.

but not in php sniplet.

thanks anyway :-)

**SOLVED** I had "/public_html/" inside of my URL, which I didn't realize wasn't needed. Once I took that out, it worked perfectly. Figured I'd just update this in case anyone in the future makes the silly mistake I did!

I'm having the same issue described here. But I'm not sure how to do enque the font face. I've followed the directions to upload the custom font (these instructions: https://docs.generatepress.com/article/adding-local-fonts/) and I've just dropped all the CSS for the fonts in a styles.css and after reading this also put them in their own font.css file.

I've tried putting this Php in (both in code snippets and in the functions file) using the url to the css file (tried both the default styles and the font.css I made) but neither seem to work.

I'm sure I'm doing something wrong. Happy to provide access to take a look.

Ah, realizing now that while it is DISPLAYING the font in the back end, the custom fonts are not in the "show advanced typography" settings in the drop down menu that shows all the fonts. Is there a way to get it in the list?

Hey Kyle - it's just an input field below, so you can type in any font name you like. The list is just a "quick select" that populates the input below it.

We'll be adding fonts to the Asset Library in Pro to make this easier very soon :)

SOrry to revive an old topic, but I have virtually the same questions.

My fonts are now loading in the block editor, but ONLY on the "desktop" preview. When viewing the mobile or tablet preview in Block Editor, they don't show up.

Here's what I have:

// show fonts in block editor
add_action( 'enqueue_block_editor_assets', function() {
    wp_enqueue_style( 'shop-controller-fonts', '/wp-content/themes/shopcontroller/style.css' );
} );

any ideas?

Can you try this snippet instead?

add_filter( 'generate_editor_styles', function( $editor_styles ) {
    $editor_styles[] = 'style.css';

    return $editor_styles;
} );

That works, thanks Ying!

Glad to hear that :)

This archived topic is closed to new replies.