Home >Web Front-end >CSS Tutorial >Managing Fonts in WordPress Block Themes
Website design hinges on typography. WordPress themes often integrate services like Google Fonts, easily managed within the Customizer for classic PHP themes. However, this isn't as straightforward for block themes. While classic theme integration is well-documented, block theme font management lacks similar guidance. This article addresses that gap. Block themes can use Google Fonts, but the registration process differs significantly from traditional methods.
Resources for integrating fonts into block themes were initially scarce. Twenty Twenty-Two, the first default block-based theme, demonstrated using downloaded font files as theme assets. This involved registering files in functions.php
and defining bundled fonts in theme.json
– a cumbersome two-step process.
Later themes, like Twenty Twenty-Three, simplified this by defining bundled fonts without explicit registration. However, manual font downloads and bundling remain, negating the convenience of hosted fonts served via a CDN.
The Gutenberg project, a plugin for testing upcoming Block and Site Editor features, offers a solution. Matias Benedetto, Gutenberg's lead architect, highlighted using Google Fonts (or any downloaded fonts) in block themes via the Create Block Theme plugin.
Learn WordPress provides a video overview of this plugin, which simplifies theme creation by offering WordPress UI controls. You can create entire themes, child themes, or style variations without coding or modifying template files. Being authored and maintained by the WordPress.org team, it's currently the best approach for Google Font integration in block themes. Note, however, that it's under active development, so expect changes.
Before exploring this new method, let's review the traditional approach for classic themes.
Articles like those from ThemeShaper (2014) and Cloudways illustrate the classic method. The Twenty Seventeen theme exemplifies enqueuing Google Fonts in functions.php
:
function twentyseventeen_fonts_url() { $fonts_url = ''; $libre_franklin = _x( 'on', 'libre_franklin font: on or off', 'twentyseventeen' ); if ( 'off' !== $libre_franklin ) { $font_families = array(); $font_families[] = 'Libre Franklin:300,300i,400,400i,600,600i,800,800i'; $query_args = array( 'family' => urlencode( implode( '|', $font_families ) ), 'subset' => urlencode( 'latin,latin-ext' ), ); $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' ); } return esc_url_raw( $fonts_url ); }
Pre-connecting Google Fonts:
function twentyseventeen_resource_hints( $urls, $relation_type ) { if ( wp_style_is( 'twentyseventeen-fonts', 'queue' ) && 'preconnect' === $relation_type ) { $urls[] = array( 'href' => 'https://fonts.gstatic.com', 'crossorigin', ); } return $urls; } add_filter( 'wp_resource_hints', 'twentyseventeen_resource_hints', 10, 2 );
This method has a significant drawback. A 2022 German court ruling highlighted GDPR violations due to the exposure of visitor IP addresses when enqueuing Google Fonts. This issue is addressed by the Create Block Theme plugin.
The modern approach utilizes the Create Block Theme plugin. Set up a local development environment (e.g., using Flywheel Local) and install the Theme Test Data and Create Block Theme plugins.
Navigate to Appearance → Manage theme fonts. This screen lists defined fonts and offers options to:
Using a blank theme (e.g., Emptytheme, renamed for clarity), adding a Google Font (like Inter) automatically downloads and stores it in the theme's assets/fonts
folder, updating theme.json
:
View theme.json code
function twentyseventeen_fonts_url() { $fonts_url = ''; $libre_franklin = _x( 'on', 'libre_franklin font: on or off', 'twentyseventeen' ); if ( 'off' !== $libre_franklin ) { $font_families = array(); $font_families[] = 'Libre Franklin:300,300i,400,400i,600,600i,800,800i'; $query_args = array( 'family' => urlencode( implode( '|', $font_families ) ), 'subset' => urlencode( 'latin,latin-ext' ), ); $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' ); } return esc_url_raw( $fonts_url ); }
The font is then available in the Site Editor's Global Styles.
Adding local fonts works similarly, simplifying the process compared to manual functions.php
modifications. Font removal is also handled within the plugin's interface.
The "Font Manager" feature is planned for WordPress core, offering similar functionality.
The Create Block Theme plugin streamlines font management in WordPress block themes, providing a user-friendly and GDPR-compliant solution. It simplifies adding, removing, and managing fonts without direct code interaction.
(List of resources remains the same, reformatted for better readability)
WordPress Font Management:
GitHub Issues:
European GDPR Requirements:
The above is the detailed content of Managing Fonts in WordPress Block Themes. For more information, please follow other related articles on the PHP Chinese website!