Archived topic
Javascript
3 replies · Started by Janine on September 16, 2020
Hiya.
How can I embed Javascript code in my theme's header scripts box? And are there any negative consequences to doing so?
Thank you.
Hi Janine,
How can I embed Javascript code in my theme’s header scripts box? ..
To clarify, did you mean <head> tag or the header section of the theme? These are 2 different things.
If you meant <head>, you can try this PHP code to add your javascript code.
function hook_javascript() {
?>
<script>
'Add your script here'
</script>
<?php
}
add_action('wp_head', 'hook_javascript');
For the Header element, you can try using the elements module under GP Premium and simply add your <scrip></script> there.
..And are there any negative consequences to doing so?
More scripts means more things to load. It'll affect how fast your page loads depending on the script's complexity.
For styling scripts, it can affect your page scores/rating on GTMetrix or Google PageSpeed insights if done wrong.(layout flags etc)
Other scripts may pose security risks. Make sure you completely know what the script does before adding it in to be on the safe side.
Hi, Elvin.
I am not sure what I mean. Let me double check and get back with you. And thank you!
Sure, take your time.
No problem.:)