WordPress.org

Forums

How-to add a script to the header tag (11 posts)

  1. NogNeetMachinaal
    Member
    Posted 1 year ago #

    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

  2. Andrew
    Nuh uh moderator
    Posted 1 year ago #

    Are you using your own theme?

  3. NogNeetMachinaal
    Member
    Posted 1 year ago #

    Thanks for the quick response!

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

  4. Andrew
    Nuh uh moderator
    Posted 1 year ago #

    Do you have a functions.php file?

  5. NogNeetMachinaal
    Member
    Posted 1 year ago #

    Yes - there is such a file.

  6. wiresplus
    Member
    Posted 1 year ago #

    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.

  7. Andrew
    Nuh uh moderator
    Posted 1 year ago #

    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' );
  8. NogNeetMachinaal
    Member
    Posted 1 year ago #

    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.

  9. Andrew
    Nuh uh moderator
    Posted 1 year ago #

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

  10. NogNeetMachinaal
    Member
    Posted 1 year ago #

    @Andrew:
    In this case I'm the hosting provider... :-)

  11. Andrew
    Nuh uh moderator
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic