Keep Only the Theme Fonts You Use
Removes the font families you do not use from the theme's font list, so editors see a short list and the unused fonts are never loaded.
Opens WPIDE on
Code
// Font family slugs to keep.
$wpide_keep_font_slugs = array( 'manrope', 'fira-code' );
add_filter( 'wp_theme_json_data_theme', function ( $theme_json ) use ( $wpide_keep_font_slugs ) {
$data = $theme_json->get_data();
$families = isset( $data['settings']['typography']['fontFamilies'] ) ? (array) $data['settings']['typography']['fontFamilies'] : array();
$families = isset( $families['theme'] ) ? (array) $families['theme'] : $families;
if ( ! $families ) {
return $theme_json;
}
$kept = array_values( array_filter( $families, function ( $family ) use ( $wpide_keep_font_slugs ) {
return isset( $family['slug'] ) && in_array( $family['slug'], $wpide_keep_font_slugs, true );
} ) );
// Never leave the theme without fonts because of a typo.
if ( ! $kept ) {
return $theme_json;
}
return $theme_json->update_with( array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
'settings' => array( 'typography' => array( 'fontFamilies' => $kept ) ),
) );
} );
Notes
Font slugs are shown in the theme's theme.json (settings.typography.fontFamilies). Text set to a removed font falls back to the default font.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.