How to stop loading multiple copies of jquery

My wordpress header.php and function.php is given below.please help me to modify my php code to stop problems due to loading jquery multiple times. My plugins and themes are loading jquery multiple times.

My files
Header.php
Function.php


All experts are telling that i need to include enqueue script !!! I read different documents related to it.

I’m new to coding , i dont know where to add this enqueue script, what type of code I need to add?

2 Answers
2

You should remove these lines from your header.php

<script language="javascript" type="text/javascript" src="https://wordpress.stackexchange.com/questions/154191/<?php bloginfo("template_url'); ?>/javascripts/jquery.js"></script>
<script language="javascript" type="text/javascript" src="https://wordpress.stackexchange.com/questions/154191/<?php bloginfo("template_url'); ?>/javascripts/tabber.js"></script>
<script language="javascript" type="text/javascript" src="https://wordpress.stackexchange.com/questions/154191/<?php bloginfo("template_url'); ?>/javascripts/superfish.js"></script>

And instead add the following function in your functions php…
right at the top.

wp_enqueue_script

function sg_theme_js(){

    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'tabberjs', get_template_directory_uri().'/javascripts/tabber.js', array('jquery'), 1.0, true);
    wp_enqueue_script( 'superfishjs', get_template_directory_uri().'/javascripts/superfish.js', array('jquery'), 1.0, true);

}
add_action( 'wp_enqueue_scripts', 'sg_theme_js' );

Explenation:
Since many plugins use jQuery (and other js files) they need to know (and wordpress too) that you already loaded jQuery… but, they cannot check it after the page has loaded meaning by checking your header becouse that is too late!

That is why we should enqueue scripts and those allow plugins to depened on them.

Now… a small comment.
if you wish to manage your theme meaning deal with code you need to learn this stuff 😉 – take a moment to read about wp_enqueue_script.

Here is additional information about: wp_enqueue_script

Leave a Comment