0

I've been googling for days and I didn't find a solution. I need to add a JS code to the head of all my pages, and I wanna do that manually (without a plugin).

I found a '~/wp-content/themes//header.php' and this in fact add my code to the head, but only to the landing page of the blog, not to the rest of the entries.

One hint I've found (inside the file mentioned before) is that there is a '' and I think if I find where it loads this function I could come up with a solution.

Additional info: the code I need to embed to all pages is a little script that loads a font from an external source.

Any other ideas on how to face this are welcome.

Thanks a lot.

  • Your question is little confusing to me! If you wanna run script then just put it in head section, But I am not getting meaning of second and third paragraph in your question. – Rishabh Nov 7 '16 at 10:40
2

You might use the wp_head action, That is triggered within the section of the user's template by the wp_head() function.

add_action("wp_head", "hook_js");

function hook_js() {
if(is_home() || is_front_page(){
    $output="<script> alert('Page is loading...'); </script>";

    echo $output;
    }
 }

Note that is better to add style and script with the action wp_enqueue_scripts and then enqueue and register them with their proper functions (wp_register_script & wp_enqueue_script)

  • I suppose the statements 'add_action()' and 'function xxx()' have to be included in '~/wp-content/themes/header.php' ? And thanks a lot for your reply, I found some information related to these programming syntax but I get lost easily. – viktta Nov 8 '16 at 8:21
  • No, everything goes in the functions.php of your theme. – Benoti Nov 8 '16 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.