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?

  • 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. – kaiser Jul 10 '14 at 16:52
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.

  • 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. – Jithin Jul 10 '14 at 17:21
  • can you tell me where to place enqueue code in my php file? – Jithin Jul 11 '14 at 2:07
  • it should go in your theme's functions.php file. – Milo Jul 11 '14 at 2:21
  • so you are telling that I should remain my header.php as it is? – Jithin Jul 11 '14 at 2:36
  • no, you need to remove the script tags and enqueue those scripts instead. – Milo Jul 11 '14 at 3:54

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.