• Hi Anders,

    due to recent verdicts here in Germany, I would prefer to host the Google Fonts that I use locally.

    I have already generated a Chaplin child theme (using the Generate Child Theme plugin), uploaded the necessary font files to my server and edited the child theme’s styles.css accordingly.

    I have then tried to get rid of the Google Fonts references by installing the Disable/Remove Google Fonts plugin. Unfortunately, it does not seem to work with Chaplin.

    Are you aware of another plugin that would do the job or could you tell me how to do it manually instead?

    Thanks in advance and best wishes,
    Thomas

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Theme Author Anders Norén

    (@anlino)

    Hi @thomascloer,

    Add the following code to the functions.php file in your child theme, and Google Fonts should be removed:

    add_filter( 'chaplin_css_dependencies', function( $css_dependencies ) {
    
    	if ( isset( $css_dependencies['chaplin-google-fonts'] ) ) {
    		unset( $css_dependencies['chaplin-google-fonts'] );
    	}
    
    	return $css_dependencies;
    
    } );

    Hi Anders,

    Because you are adding the fonts as a dependancy to the primary stylesheet it’s not possible to deregister the fonts in the regular WordPress way without taking down the main stylesheet too.

    function dc_remove_fonts() {
    	wp_deregister_style( 'chaplin-google-fonts' );
    }
    add_action( 'wp_enqueue_scripts', 'dc_remove_fonts', 100);

    Instead a more complex function is required:

    function dc_remove_fonts( $deps ) {
    	if ( ( $key = array_search('chaplin-google-fonts', $deps ) ) !== false) {
    	    unset( $deps[$key] );
    	}
    	return $deps;
    }
    add_filter( 'chaplin_css_dependencies', 'dc_remove_fonts', 10, 1 );

    What are the benefits to registering the fonts as a dependency rather than their own entity? And are they worth the trade off of removing the core wp_deregister_style functionality?

    Theme Author Anders Norén

    (@anlino)

    @dannycooper I probably would have done it differently today, but since the style.css file reference the fonts added by the Google Fonts enqueue, it seemed reasonable that the Google Fonts should be considered a dependency of the main stylesheet.

    For your plugin, wouldn’t it be possible to modify the values of $wp_styles to remove all mentions of Google Fonts in the dependencies?

    Yes, it’s definitely possible to add an exception for this theme and your others that use the same method. I can think of a few ways to achieve that. I just wanted to check in and see if anything would change in the theme.

    Also, I don’t think this works as unset works on the index not the value:

    add_filter( 'chaplin_css_dependencies', function( $css_dependencies ) {
    
    	if ( isset( $css_dependencies['chaplin-google-fonts'] ) ) {
    		unset( $css_dependencies['chaplin-google-fonts'] );
    	}
    
    	return $css_dependencies;
    
    } );

    Instead I think you need to use the more complex version I posted above.

    Thread Starter Thomas Cloer

    (@thomascloer)

    Thank you, guys!

    For the time being, I have added Danny’s more complex funtion to my child theme’s function.php, and everything seems to work as expected.

    Have a great afternoon!

    Best wishes,
    Thomas

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Google Fonts locally’ is closed to new replies.