$ is not defined: using jQuery in the php files

when i try to use jquery inside a php template file

  $(function() {
    console.log( "ready!" );

im getting this error:
Uncaught ReferenceError: $ is not defined

What am i doing wrong? Under my /js/_custom.js file everything works fine.


jQuery is loaded in noconflict mode with WordPress


1 Like

You shouldn’t be using jQuery inside a PHP template anyway. Learn to love wp_localize_script() :wink:

1 Like

^ :thumbsup: (what he said)

1 Like

Hey, thanks!
I read about it and i think i got the point. But i don’t understand how to use jquery then. Am i suppose to wirte JS files for each action?

Ideally all of your JS will be written in _main.js and then concatenated with your other scripts (Bootstrap’s and other plugins’ JS) and linted/uglified/minified into scripts.min.js so that it’s all in one place and loaded in the theme’s footer in one request.

WordPress itself doesn’t do a great job of separating JS and PHP so it’s easy to think that JS in the middle of a PHP template is ok. There may be some cases where it’s necessary but it’s a good idea to keep them separate when you can.