-
AuthorPosts
-
October 29, 2014 at 4:54 am #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
October 29, 2014 at 6:22 am #342562There are many ways to do this.
Adding a Google Font
1ST OPTION: enqueue it via the child themefunctions.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.October 29, 2014 at 7:48 am #342585Thanks 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
October 29, 2014 at 7:52 am #342589Hi 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
October 29, 2014 at 8:12 am #342595Hi 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?
October 29, 2014 at 8:22 am #342599Fantastic, this is great as I will reduce my plugins by 1…much appreciated Tung Do.
October 31, 2014 at 5:19 am #343865Hi!
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,
IsmaelNovember 3, 2014 at 5:06 am #345058Thank you Ismael, I assume this adds the font as a choice in the theme options font lists. Much appreciated to you both.
cheers
Darryl
November 4, 2014 at 4:46 am #345712 -
AuthorPosts
- The topic ‘Function for adding additional head html’ is closed to new replies.