I’m using below method inside WordPress admin to use jQuery library and this is working fine and I don’t need to add source for jQuery library for this method.
<script>
jQuery(document).ready(function(){
// works inside WordPress admin without source
});
</script>
But the same method does not work on front end WordPress form and to make it work I’ve to add source of library?
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
jQuery(document).ready(function(){
// need to add jQuery source
});
</script>
Why do I need to add source of jQuery library for frontend? How can load WordPress JQuery library to frontend?
It works in the admin, because there are admin scripts that have jQuery as a dependency and WordPress includes jQuery in order for those scripts to work.
You should use wp_enqueue_script() function when adding scripts (not including them in scripts tags). Notice the
$deps
variable of the function, if you set that toarray( 'jquery' )
, WordPress will include the library for you, so your script can use it.If you scroll down the page you can see which libraries are included in WordPress and what handles are used to enqueue them.
You first need to ensure that jQuery is enqueued. it should be but just to ensure it is add the following in the header.php
after that you can add your script as follow