Dequeue google fonts

How do I dequeue or prevent google fonts from loading in X?

I want to be able to serve them via a CDN as per these instructions: https://www.keycdn.com/blog/google-fonts-cdn/

Hi There,

Please add the following code under functions.php file locates in your child theme:

function x_enqueue_site_styles() {

    //
    // Stack data.
    //

    $stack  = x_get_stack();
    $design = x_get_option( 'x_integrity_design' );

    if ( $stack == 'integrity' && $design == 'light' ) {
      $ext = '-light';
    } elseif ( $stack == 'integrity' && $design == 'dark' ) {
      $ext = '-dark';
    } else {
      $ext = '';
    }



    //
    // Enqueue styles.
    //

    wp_enqueue_style( 'x-stack', X_TEMPLATE_URL . '/framework/css/dist/site/stacks/' . $stack . $ext . '.css', NULL, X_ASSET_REV, 'all' );

    do_action( 'x_enqueue_styles' );

    if ( is_child_theme() && apply_filters( 'x_enqueue_parent_stylesheet', false ) ) {
      $rev = ( defined( 'X_CHILD_ASSET_REV' ) ) ? X_CHILD_ASSET_REV : X_ASSET_REV;
      wp_enqueue_style( 'x-child', get_stylesheet_directory_uri() . '/style.css', array(), $rev, 'all' );
    }

    if ( is_rtl() ) {
      wp_enqueue_style( 'x-rtl', X_TEMPLATE_URL . '/framework/css/dist/site/rtl/' . $stack . '.css', NULL, X_ASSET_REV, 'all' );
    }

    if ( X_BBPRESS_IS_ACTIVE ) {
      if ( x_is_bbpress() ) {
        wp_deregister_style( 'buttons' );
      }
      wp_deregister_style( 'bbp-default' );
      wp_enqueue_style( 'x-bbpress', X_TEMPLATE_URL . '/framework/css/dist/site/bbpress/' . $stack . $ext . '.css', NULL, X_ASSET_REV, 'all' );
    }

    if ( X_BUDDYPRESS_IS_ACTIVE ) {
      wp_deregister_style( 'bp-legacy-css' );
      wp_deregister_style( 'bp-admin-bar' );
      wp_enqueue_style( 'x-buddypress', X_TEMPLATE_URL . '/framework/css/dist/site/buddypress/' . $stack . $ext . '.css', NULL, X_ASSET_REV, 'all' );
    }

    if ( X_WOOCOMMERCE_IS_ACTIVE ) {
      wp_deregister_style( 'woocommerce-layout' );
      wp_deregister_style( 'woocommerce-general' );
      wp_deregister_style( 'woocommerce-smallscreen' );
      wp_enqueue_style( 'x-woocommerce', X_TEMPLATE_URL . '/framework/css/dist/site/woocommerce/' . $stack . $ext . '.css', NULL, X_ASSET_REV, 'all' );
    }

    if ( X_GRAVITY_FORMS_IS_ACTIVE ) {
      wp_enqueue_style( 'x-gravity-forms', X_TEMPLATE_URL . '/framework/css/dist/site/gravity_forms/' . $stack . $ext . '.css', NULL, X_ASSET_REV, 'all' );
    }

    if ( X_CONTACT_FORM_7_IS_ACTIVE ) {
      wp_deregister_style( 'contact-form-7' );
    }

}

Hope it helps :slight_smile:

Great, thank you. I don’t understand what that code does but it worked!

I’d also like to dequeue the font awesome font so I can have it loaded on my CDN. How can I add my CDN url to this or remove it and then add it back with the CDN’s url?

I just noticed the code you gave is triggering 2 errors:
Use of undefined constant X_ASSET_REV - assumed ‘X_ASSET_REV’

How do I fix this?

Hi there,

Please add this line before/above function x_enqueue_site_styles() {

define( 'X_ASSET_REV', '5.1.1' );

And make sure to change the version every time you update your theme, it should be equal to the version of the main theme. It’s for cache purposes.

Thanks!

Ok, will do. Could this not be automatically set, using some code like:

<?php $my_theme = wp_get_theme(); echo $my_theme->get( 'Name' ) . " is version " . $my_theme->get( 'Version' ); ?>

Hi there,

There is a similar function in X but it’s used internally. And also, it won’t be accessible since the parent theme is loaded only after the child theme. And you can’t call any function that isn’t loaded first, the same reason X_ASSET_REV is triggering that notice because it’s not yet loaded.

Thanks.

I’d also like to dequeue the font awesome font so I can have it loaded on my CDN. How can I add my CDN url to this or remove it and then add it back with the CDN’s url?

Hi there,

I’m not sure what you mean, but it should be only configurable with the CDN plugin you’re using. Please check your plugin’s manual. The plugin should able to pickup the assets that it can serve through CDN.

Thanks.

Ok, can you just tell me how to remove/dequeue the font awesome font from X so it doesn’t load on my site?

Hi there,

We don’t recommend it as it may break the font system relying on it. But you may add this code to your child theme’s functions.php


add_action('init','no_google_fonts', 999999);

function no_google_fonts () {

wp_dequeue_style( 'x-google-fonts');

}

Thanks!

3 posts were split to a new topic: RE: Dequeue Google Fonts