2

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?

1
  • 2
    Vijitha, please do not use services like pastebin or similars. In case they go down, nobody will understand your problem. Please file an edit and put the code where it belongs: In your question. Commented Jul 10, 2014 at 16:52

2 Answers 2

3

You should remove these lines from your header.php

<script language="javascript" type="text/javascript" src="<?php bloginfo('template_url'); ?>/javascripts/jquery.js"></script>
<script language="javascript" type="text/javascript" src="<?php bloginfo('template_url'); ?>/javascripts/tabber.js"></script>
<script language="javascript" type="text/javascript" src="<?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

1

The simple answer is to never add script tags directly to template files. This is exactly why the WordPress script registration and enqueue system exists.

6
  • I already read the document you suggested, but it is difficult to understand for me.can you tell me the code i needed to edit in above php files. Commented Jul 10, 2014 at 17:21
  • can you tell me where to place enqueue code in my php file? Commented Jul 11, 2014 at 2:07
  • it should go in your theme's functions.php file. Commented Jul 11, 2014 at 2:21
  • so you are telling that I should remain my header.php as it is? Commented Jul 11, 2014 at 2:36
  • no, you need to remove the script tags and enqueue those scripts instead. Commented Jul 11, 2014 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.