0

I don't know how to do it, but I have a few js code blocks in my sites header like the StatCounter tracking code, facebook like button js code, etc.

Now the thing is that I want to combine all the external js code blocks into one file so that I can add it either in the header or footer section. Can any one guide me on how can I add these code blocks into a single file?

0

Just chuck them in a file, say your-theme/js/global.js and enqueue it in functions.php:

function wpse_180397_scripts() {
    wp_enqueue_script(
        'wpse-180397-global', /* Unique ID */
        get_bloginfo( 'template_url' ) . '/js/global.js', /* URL */
        array(), /* Dependencies */
        true /* In footer */
    );
}

add_action( 'wp_enqueue_scripts', 'wpse_180397_scripts' );
0
  1. create a new javascript (.js) file with any text editor you'd choose.
  2. start it with jQuery(document).ready(function() {
  3. copy and paste all other code blocks you've got, with out the <script> start and end tags.
  4. if you have jQuery(document).ready(function() { in the beginning of one of these code blocks you should consider removing, but it carefully. don't forget to remove also the } end tag;
  5. if you followed step 2, your javascript file should end up with }.
  6. close the file and save it with any name you'd choose. lets say global.js.
  7. finally add the function to functions.php file as TheDeadMedic suggested.

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.