[Resolved] Cannot redeclare tu_add_local_fonts()

Home Forums Support [Resolved] Cannot redeclare tu_add_local_fonts()

Home Forums Support Cannot redeclare tu_add_local_fonts()

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #751813
    Danielle

    Hi! So I’m trying to add some fonts to the Customizer. I was hoping there was a nice easy method for code illiterates like me, but I could only find this rather complicated how-to document: https://docs.generatepress.com/article/adding-local-fonts/. An hour and a headache later, I was able to follow the directions and install one new font (and I’m pretty darn proud of myself, thank you very much ;). I tried to do the exact same thing a second time to install another one, but it tells me I can’t have the “tu_add_local_fonts” line twice in the function.php file. The how-to document didn’t say anything about not being able to use that line more than once. So what am I doing wrong? How do I get around that so I can install a few more?

    Here’s what I successfully added to the functions.php file the first time after adding the @font-face CSS to the stylesheet:

    add_filter( ‘generate_typography_default_fonts’, ‘tu_add_local_fonts’ );
    function tu_add_local_fonts( $fonts ) {
    $fonts[] = ‘Charmonman’;
    return $fonts;
    }

    And the second time, unsuccessfully:

    add_filter( ‘generate_typography_default_fonts’, ‘tu_add_local_fonts’ );
    function tu_add_local_fonts( $fonts ) {
    $fonts[] = ‘Parisienne’;
    return $fonts;
    }

    But when I try to save the second filter, it tells me:

    “Your PHP code changes were rolled back due to an error on line 26 of file wp-content/themes/generatepress_child/functions.php. Please fix and try saving again.

    Cannot redeclare tu_add_local_fonts() (previously declared in wp-content/themes/generatepress_child/functions.php:17)”

    Which is even more confusing because line 26 is just the closing “}”. Line 24 is where the repeat line is.

    Can anyone help me? (And is there really no easier way to install fonts?!) Let me know if this was too confusing or if you need other information. Thanks!

    #751889
    David
    Staff
    Customer Support

    Hi there,

    the error is because you are declaring the same function twice ( which is a no no ๐Ÿ™‚ ), so instead use one function and declare mutliple fonts within it e.g

    add_filter( 'generate_typography_default_fonts', function( $fonts ) {
        $fonts[] = 'Charmonman';
        $fonts[] = 'Parisienne';
    
        return $fonts;
    } );

    You can add as many as you like. You still need multiple @font-face blocks.

    #752225
    Danielle

    Ah-ha! I figured that’s what I needed to do but I didn’t know how to write it. Thank you so much, it worked!

    Someone should probably make a note of this caveat in that document.

    #752267
    David
    Staff
    Customer Support

    You’re welcome ๐Ÿ™‚

    #752364
    David
    Staff
    Customer Support

    FYI – Docs updated here to include multiple fonts in the array:

    https://docs.generatepress.com/article/adding-local-fonts/#adding-it-to-the-customizer

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.