P
P
Programmir2018-05-21 17:12:30
WordPress
Programmir, 2018-05-21 17:12:30

How to include jquery in wordpress?

I'm learning Wordpress and just got bored with jquery. In footer.php connected via cdn, but the jquery code did not work. Then in functions.php I wrote wp_enqueue_script( 'jquery'); The script was included in head. But the code still didn't work. Tried to re-register jquery using cdn. As a result, left wp_enqueue_script( 'jquery'); And I wrapped the whole jquery code in jQuery(document).ready(function($) {. Part of the jquery code worked, but the click event did not work as it should, the page just reloads, although I have .on("click", function( e) {
e.preventDefault(); ... And in normal layout everything worked fine. But with wordpress you can't just include jquery to make it work fine.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2018-05-21
@azerphoenix

1) scripts and styles are connected via functions.php
2) jQuery is already in the VI, if you want to remove it and add your version (more recent for example), then first do wp_deregister_script()
Example:

function register_styles_scripts() {
wp_deregister_script('jquery');
  wp_register_script('jquery', get_template_directory_uri() .
    '/js/jquery-3.2.1.min.js');
  wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'register_styles_scripts');

Don't forget that if you use bootstrap, then jquery, popper are connected before the bootstrap script.
Another point is to look at the jquery code itself. If there are $ characters then replace with jQuery
or use the following:
$.noConflict();
jQuery( document ).ready(function( $ ) {

.........

});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question