Support » Fixing WordPress » How-to add a script to the header tag

  • Hi Everyone,

    I’m not sure if I use the right forum for this topic.

    But for the purpose of performance monitoring, I’m looking for a way to add a script in the header tag of a web page without using a plugin or theme.

    I’m running WordPress 3.8.1 on Apache 2.2.25 (Win32) and PHP 5.4.20.

    What does it take to make that happen?

    Thanks – Will

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    Are you using your own theme?

    Thanks for the quick response!

    To answer your question:
    Sort of – I’m using child themes.
    Typically based on one of the Themify themes.

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    Do you have a functions.php file?

    Yes – there is such a file.

    Just pop your script in header.php

    Of course, you should do this in your child header so it doesn’t go AWOL when WP ubdates.

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    Put your JavaScript into a (.js) file.

    Then load that file through your functions.php file by using the “wp_enqueue_script” function.
    http://www.codex.wordpress.org/Function_Reference/wp_enqueue_script

    E.g.:

    function myCustomScript() {
    
           // My custom-script.js file is at the following path /wp-content/themes/my-child-theme/js/custom-script.js
    
           // Note you'll have to use "get_stylesheet_directory_uri()" if you're using a child theme and the script is in your child theme
    
            wp_enqueue_script( 'a-script-that-does-this', get_stylesheet_directory_uri() . '/js/my-custom-script.js', '', '1.0' );
    
    }
    
    add_action( 'wp_enqueue_scripts', 'myCustomScript' );

    Thanks guys – great!

    Please allow me to go a bit off-topic here… 🙂
    Would you know a way to do the same in Apache and Tomcat?

    There are two reasons for an approach like that.
    (1) – I expect it to work regardless the CMS I’m working with; the same expectation are for Tomcat and Java applications.
    (2) – I’m able to start this as early as possible => includes monitoring the performance of the CMS itself.

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    Sounds like you need to contact your hosting providers about this. We can only give WordPress support here.

    @andrew:
    In this case I’m the hosting provider… 🙂

    Moderator Andrew Nevins

    (@anevins)

    Forum moderator

    Then maybe an Apache and Tomcat forum is better suited for you.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How-to add a script to the header tag’ is closed to new replies.