1

I want to run a JavaScript and I want to paste in inside the <head></head> tag

I do not want to add it to the text-view area.

How can I add this code snippet to an individual post (I mean not editing the headers.php)?

  • Google about these terms wp_head, is_page(), is_single()! – Sumit Apr 15 '16 at 6:05
3

You want to hook into the Header code using something like this:

add_action('wp_head','hook_javascript');

function hook_javascript() {
    if (is_single('123')) { 
        $output="<script> enter your script here </script>";
        echo $output;
    }
}

More information available here: https://codex.wordpress.org/Plugin_API/Action_Reference/wp_head

  • 1
    As @cybmeta pointed out on another answer, it would probably be better using wp_enqueue_scripts if you're using a javascript file rather than a few lines of code. – TomC Apr 15 '16 at 7:43
  • This is the correct answer, but just to add to it in case the original poster isn't aware, the place to put this code is in your theme's functions.php – Tim Malone Apr 15 '16 at 10:40

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.