0

I'd like to add the Latest jQuery from WordPress. But there has no javascript string ($) in my index.php file which I could change and write "jQuery" instead of "$". This is all jquery plugins which I have used in index.php file

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url');?>/js/jquery.easing.1.3.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url');?>/js/jquery.prettyPhoto.js"></script>
<script src="<?php bloginfo('template_url');?>/js/jquery.sticky.js"></script>
<script src="<?php bloginfo('template_url');?>/js/jquery.isotope.min.js" type="text/javascript"></script>
<script type="text/javascript" src="<?php bloginfo('template_url');?>/js/sorting.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url');?>/js/jquery.jcarousel.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url');?>/js/js.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url');?>/js/jquery.stellar.min.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url');?>/js/waypoints.min.js"></script>

<!--[if lt IE 9]>
    <script src="<?php bloginfo('template_url');?>/js/html5shiv.js"></script>
    <script src="<?php bloginfo('template_url');?>/js/respond.src.js"></script>
<![endif]-->

I wrote this function in functions.php file for calling Latest jQuery from WordPress-

/* Adding Latest jQuery from WordPress *************************/

function polish_arest_jquery() {
    wp_enqueue_script('jquery');
}
add_action('init', 'polish_arest_jquery');

I am sorry to say that jQuery noConflict Wrappers is not available in my index.php file.

In that case, how I could add Latest jQuery from WordPress? Thank you.

Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
active-worker
  • 56
  • 1
  • 1
  • 8
  • 2
    It's not clear at all what you're asking for, but generally you should be using `wp_enqueue_script` to add scripts, and then add jQuery as a dependency, and Wordpress will add the latest version automagically. Also, jQuery is always in no-conflict mode in Wordpress, so you have to use a no-conflict wrapper. It's all very well explained in the [**Codex**](http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers) – adeneo Sep 07 '14 at 09:37

1 Answers1

0

Yes, you should enqueue the scripts using the wp_enqueue_script function, and the script will add to where the wp_header() hook calls.

And the jQuery library is automatically included in the WordPress kernel, the newest WordPress edition will includes the latest jquery 1.X, and it was also registered to the script queue.

You can add any script as the following way in the theme function.php, just like what the twentyfourteen theme does.

function theme_scripts() {
    wp_enqueue_script( 'theme-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20140319', true );
}
add_action( 'wp_enqueue_scripts', 'theme_scripts' );

The above means, import the /js/functions.js script, but it requires jquery to be imported first, thus the jquery was imported automatically.


More, if you use the wp_enqueue_script to make the script render correctly, don't forget the wp_head(); hook trigger within the <head></head> tag.

Hope this would help! Good luck.


More, if you only troubled by the name, want jQuery to be $, you can use the following strategy:

jQuery(function($) {
    $('#what_ever_you_want').do_something();
});

See another question: https://stackoverflow.com/a/24352348/2544762

Community
  • 1
  • 1
Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
  • Hi @fish_ball, thanks for your answer. Could you please share not just one line `wp_enqueue_script( 'twentyfourteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20140319', true );` but the whole code? – active-worker Sep 07 '14 at 12:55
  • @active-worker of course, see my updated answer. Just put the code inside a function, and add the function on the hook `wp_enqueue_scripts`. So it will run when the wordpress boots. – Alfred Huang Sep 07 '14 at 13:29