Bartłomiej Tomaja Bartłomiej Tomaja - 6 months ago 12
jQuery Question

wordpress add own javascript file. no reaction

I add to children template directory in scripts.php code:

<?php

function load_styles_and_scripts() {

wp_register_script (
'myScript',
get_template_directory_uri().'/script.js',
array('jquery'),
'1.11.0',
true
);

wp_enqueue_script ('myScript');
}

add_action('wp_enqueue_scripts', 'load_styles_and_scripts');


my script.js file contain:

$( document ).ready(function() {
console.log( "ready!" );
alert("Hello! I am an alert box!");
});


folders / paths

I set my optional theme in wordpress as active one.
CSS is loading correct but javascript from script.js not - is no making any differences on site.

I fight all day with this.
Thanks straight away ;)

Script is loaded - firebug

Answer

To use jQuery with WordPress, you must use the string 'jQuery' instead of '$'. For example:

jQuery('#id').show();

If you wish to override the jQuery function and use '$', checkout this reference https://digwp.com/2011/09/using-instead-of-jquery-in-wordpress/. So for your example do something like this:

jQuery(document).ready(function() {
    console.log('ready!');
    alert('Hello! I am an alert box!');
});