Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #342546

    Gday Kriesi

    Do you have a function that I can use in my child theme functions.php that will enable me to add extra information into the head? For example, adding a Google font or a custom script?

    Thanks for any help you can provide here.

    cheers

    Darryl

    #342562

    There are many ways to do this.

    Adding a Google Font
    1ST OPTION: enqueue it via the child theme functions.php file. Example:

    add_action( 'wp_enqueue_scripts', 'enfold_child_load_google_fonts' );
    function enfold_child_load_google_fonts() {
    	wp_register_style( 'enfold-child-fonts', '//fonts.googleapis.com/css?family=Lora:400' );
    	wp_enqueue_style( 'enfold-child-fonts' );
    }

    2ND OPTION: embed the Google Font using your child theme style.css file. (The example above is recommended.) Example:
    @import url(https://fonts.googleapis.com/css?family=Open+Sans);

    3RD OPTION: Use the Google Font embed link in your child theme header.php file. Again, I recommend option one.

    Adding scripts
    Very similar, almost exactly, to the examples above. Look into wp_enqueue_scripts. At the bottom of that documentation page, there are links to other functions that should help you as well, like print, register, or localize scripts.

    #342585

    Thanks for your detailed response Tung Do, I’d recognised that the child theme functions.php method would suit best so I am keen to try the first option you have shown. I really appreciate your helpfulnes here, thank you.

    cheers

    Darryl

    #342589

    Hi Tung Do,

    I tried Option 1 and receive an error:

    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘enfold_child_load_google_fonts’ not found or invalid function name in /home/integral/public_html/wp-includes/plugin.php on line 505

    I’m not a php coder so am not sure what it means nor what to do…do you recognise this code and know how to fix it?

    cheers

    Darryl

    #342595

    Hi Darryl. My mistake. The following should work:

    add_action( 'wp_enqueue_scripts', 'enfold_child_load_google_fonts' );
    function enfold_child_load_google_fonts() {
    	wp_register_style( 'enfold-child-fonts', '//fonts.googleapis.com/css?family=Lora:400' );
    	wp_enqueue_style( 'enfold-child-fonts' );
    }

    See how the second line now matches a part of the first line?

    #342599

    Fantastic, this is great as I will reduce my plugins by 1…much appreciated Tung Do.

    #343865

    Hi!

    Thank you for using Enfold.

    @Tung Do: Thanks! Very helpful. :)


    @itchybrain
    : Aside from the suggestions above, you can use the wp_head action hook function. Regarding the google fonts, the theme have a particular filter for that. Use this on functions.php:

    add_filter( 'avf_google_heading_font',  'avia_add_heading_font');
    function avia_add_heading_font($fonts)
    {
    $fonts['Source Sans Pro'] = 'Source Sans Pro:400,600,800';
    return $fonts;
    }
    
    add_filter( 'avf_google_content_font',  'avia_add_content_font');
    function avia_add_content_font($fonts)
    {
    $fonts['Source Sans Pro'] = 'Source Sans Pro:400,600,800';
    return $fonts;
    }

    Change the google font name and specify the correct font weights.

    Best regards,
    Ismael

    #345058

    Thank you Ismael, I assume this adds the font as a choice in the theme options font lists. Much appreciated to you both.

    cheers

    Darryl

    #345712

    Hi!

    Yes, it will add the font as available font option on Enfold > General Styling > Fonts panel.

    Regards,
    Ismael

Viewing 9 posts - 1 through 9 (of 9 total)
  • The topic ‘Function for adding additional head html’ is closed to new replies.