• I am developing a child theme from the theme Garfunkel. The parent theme calls several fonts from Google, and does so in functions.php. I don’t want them for several reasons: (1) I need diacritics found in very few fonts (and not included in Google’s offerings); and (2) for speed. The functions.php BEGINS to call the fonts after almost everything else is done–so the last part of loading is just them. (I’m looking at the timeline on GTMetrix’s analysis of the site–though theory would predict this also.)

    If I do something to the child functions.php, it would run AFTER the parent’s functions.php, and so all the time wasted calling fonts I can’t use still wasted.

    If I delete these from the parent’s functions.php, my deletion will be overwritten in any update of the parent theme.

    So: what is good practice here?

Viewing 9 replies - 1 through 9 (of 9 total)
  • From my testing, dequeueing the Google fonts stylesheet prevents WP from trying to connect to Google to load the fonts. Even if you add a bit of PHP execution time by enqueueing and then dequeueing a stylesheet, you’re more than making up for it by not calling out to Google.

    function no_more_google_fonts() {
      wp_dequeue_style( 'garfunkel-googleFonts' );
    }
    add_action( 'wp_print_styles', 'no_more_google_fonts', 11 );

    We give it a lower priority so this function will load after the parent theme enqueues the stylesheets, as wp_dequeue_stylesheet() can’t dequeue a stylesheet that hasn’t been enqueued yet.

    Thread Starter marthaleeturner

    (@marthaleeturner)

    I added the code above to my child functions.php. Looking at the page in Firefox with “Entire Cache Disabled” and “Check for Newer Version of Page Every Time”–and multiple refreshes–fonts not available on my system still in evidence.

    Testing the site so modified at gtmetrix.com, same font calls show up in Timeline.

    I conjecture your theory is that the child functions.php is “read” before the parent functions.php is enacted–so that code in the child cancels code in the parent, before any call is made. ???

    I read in the codex (under Child Themes),

    “Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)”

    Which would seem to make the parent functions.php cancel anything conflicting with it in the child functions.php.

    I read farther along,

    “TIP FOR THEME DEVELOPERS. The fact that a child theme’s functions.php is loaded first means that you can make the user functions of your theme pluggable —that is, replaceable by a child theme— by declaring them conditionally. E.g.:” [gives example code using if function exists] “In that way, a child theme can replace a PHP function of the parent by simply declaring it beforehand.”

    And I see that the parent functions.php does not include this conditional language concerning the font calls. In fact, in a rather long functions.php file, “exists” appears only once.

    Maybe this makes this particular theme a poor choice as a parent theme, for my purposes? (Can it be true that anything in a parent theme’s functions.php file that does not include conditional language cannot be overridden in the child functions.php?)

    Or have I misunderstood something here?

    Or does the code just need some small tweek? (I can write simple php code, but find WordPress php byzantine and unpredictable, difficult even to modify.)

    Can you post a link to your site with the child theme active? Also, if you’re logged in to your site, WordPress will make a call to Google to load the Open Sans font, because WordPress uses that font on the Dashboard.

    “Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)”

    Which would seem to make the parent functions.php cancel anything conflicting with it in the child functions.php.

    Yes, but no. This is why the if ( ! function_exists() ) construction works. WordPress will read the child theme, see that function foo() is declared in the child theme, read the parent theme, see if ( ! function_exists( 'foo' ), and not try to declare the parent theme’s version of function foo(). Otherwise, you would get the error “Cannot redeclare function foo() in …”.

    This particular situation is a bit of an oddball, though. Here, we’re not trying to completely override a function from the parent theme; instead, we’re adding a second function, hooked on the same action, that alters a specific part of the parent theme’s function. When WordPress encounters two differently-named functions hooked on the same action, it uses the priority system to determine which function to execute first. If no priority is declared, WordPress assumes 10. Since we’ve explicitly declared our function to be priority 11, WordPress runs the parent theme’s function first, enqueueing (among other scripts and stylesheets) the Google fonts call, then WordPress runs our function, dequeueing the Google fonts call.

    Thread Starter marthaleeturner

    (@marthaleeturner)

    Well, since Garfunkel’s functions.php file uses the word “exists” only once–i.e. this and many other things are not given the “if” construction–I decided it was not really very suitable as a parent theme.

    You can see where I got to at https://69.89.21.96/~wordturn/cosmicdance/ –around the same time I settled on a url and began developing anew at that location.

    BUT it is looking like many or most current themes have their fonts called from functions.php, so this is still an issue. Currently I am modifying the Twenty Thirteen Child Blogsonry–yeah, I know, not ideal. There, the fonts are called from the Twenty Thirteen functions.php, and rather than the “if” construction have an on/off setting, that (I presume) must be changed in the child functions.php file. Though I’m not sure what that’s supposed to look like. Fonts in WordPress, which were easy for a while (system OR called OR self-hosted) have become quite problematic due to near-hard-wiring!

    The code I posted should have eliminated the call to Google fonts. Can you post the contents of your child theme’s functions.php to Pastebin and post the link here?

    Thread Starter marthaleeturner

    (@marthaleeturner)

    It’s so short, I’ll paste it here, the whole file complete:

    <?php
    add_action( ‘wp_enqueue_scripts’, ‘theme_enqueue_styles’ );
    function theme_enqueue_styles() {
    wp_enqueue_style( ‘parent-style’, get_template_directory_uri() . ‘/style.css’ );
    }

    function no_more_google_fonts() {
    wp_dequeue_style( ‘garfunkel-googleFonts’ );
    }
    add_action( ‘wp_print_styles’, ‘no_more_google_fonts’, 11 );

    ?>

    First bit from codex–seemingly now required in a child theme (?); second bit from you.

    GTMetrix just now gives timeline with call to fonts.googleapis.com for Fira Sans, Playfair Display, and Crimson Text (in multiple weights), plus six separate calls after everything else is done, to fonts.gstatic.com for various individual ones of these font/weight combinations.

    Argh, I see what went wrong.

    Try this, instead:

    function no_more_google_fonts() {
        wp_dequeue_style( 'garfunkel_googleFonts' );
    }
    add_action( 'wp_print_styles', 'no_more_google_fonts', 11 );

    Note the underscore instead of the hyphen.

    Thread Starter marthaleeturner

    (@marthaleeturner)

    Wow–that did the trick! Thanks!

    Thread Starter marthaleeturner

    (@marthaleeturner)

    Down from 21 calls to 14. (Which matters: I’ll be adding lots with the content.) Thanks again.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘removing font call in child theme’ is closed to new replies.