Use jQuery prefix instead of $ for wordpress

Recently I was testing a jQuery code snippet to see if it would  accomplish what I needed it to accomplish. The easiest way to do this was to simply paste the jQuery code into the header.php file.

I was frustrated to find this didn’t work. My search for a solutions yielded little but a bunch of stuff about wp_enque_script. I just wanted to see if this code would fit my need not spend my entire afternoon reading up on “the proper way to do it”.

 

The quick and dirty

Long story short. The solution was to simply replace every instance of “$” in code with “jQuery”.  That done the code snippet pasted to the header.php worked  fine and I was able to confirm that it would suit my needs.

Not a production solution

Its important to note that this is not production solution but a “test” solution. In fact when you read up on wp_enque_script  you will find that their is a way to let wordpress do automatically the replacement that I did manually. But if you are in a jam and just want to test a simple piece of code this might be just what you were looking for. But be forewarned that wp_enque_script exists to avoid conflict and you will have just bypassed that entire system.  So use this only for quick tests before deciding whether or not to go through all the trouble of actually using wp_enque_script.

 

Author: Brian Marshall

Share This Post On