-2

I include some custom javascript into my wordpress theme. So I added this lines to my functions.php

function add_theme_scripts() {
    wp_enqueue_script('equalheight', get_template_directory_uri().'/js/equalheight.js', array ( 'jquery' ), true);
    wp_enqueue_script('script', get_template_directory_uri().'/js/script.js', array ( 'jquery' ), true);
}
add_action('wp_enqueue_scripts', 'add_theme_scripts');

But the scripts having problems with jquery. I will get this in my browser console:

TypeError: $ is undefined

Why? The dependence to jquery is set.

Does anyone have an idea?

Markus
  • 407
  • 2
  • 9
  • 25
  • While working jquery, for any other jquery plugin you have to first include the jquery library (Order matters). – Mayank Pandeyz Jul 19 '18 at 10:11
  • Did you make some effort in trying to find a solution by your own? Please wordpress questions post on [Wordpress Stack Exchange](https://wordpress.stackexchange.com/). I am sure that someone already had this problem. – Krzysztof Janiszewski Jul 19 '18 at 10:11
  • And here's the first google result: https://wordpress.stackexchange.com/questions/2895/not-defined-using-jquery-in-wordpress –  Jul 19 '18 at 10:14

2 Answers2

0

You have dependency for jQuery set, but you have not loaded jQuery itself:

function add_theme_scripts() {
    wp_enqueue_script("jquery");
    wp_enqueue_script('equalheight', get_template_directory_uri().'/js/equalheight.js', array ( 'jquery' ), true);
    wp_enqueue_script('script', get_template_directory_uri().'/js/script.js', array ( 'jquery' ), true);
}
add_action('wp_enqueue_scripts', 'add_theme_scripts');

Also as others have mentioned in the comments, you need to use jQuery-variable instead of $.

Esko
  • 4,109
  • 2
  • 22
  • 37
  • Thank you. I've added this line. The JQuery Script is included bevor the other scripts but nothing happends. – Markus Jul 19 '18 at 10:15
0

If you will check for the declaration of wp_enqueue_script then its like that:-

wp_enqueue_script( 
  string $handle, string $src = '', array $deps = array(), 
  string|bool|null $ver = false, bool $in_footer = false 
) 

Its third parameter is used to define the dependency and in your code, you defined that these two files need jquery to work correctly.

If you don't need jquery, use this

function add_theme_scripts() {
    wp_enqueue_script(
     'equalheight', 
      get_template_directory_uri().'/js/equalheight.js',  
      array (), true
   );
    wp_enqueue_script( 
      'script',  
      get_template_directory_uri().'/js/script.js',  
      array (), true 
    );
}
add_action('wp_enqueue_scripts', 'add_theme_scripts');
Deepak Dixit
  • 1,510
  • 15
  • 24