,

VSCode Snippet for using jQuery in WordPress

Thanks to the legacy code that is WordPress, I always wrap any jQuery I write in a simple wrapper function that makes sure the old jQuery dollar sign function $() is tacked on to the global jQuery variable. Of course, these days I’m trying to use vanilla JS as much as possible, using this website as a great resource for replacing those jQuery functions we’re all so familiar with.

When using jQuery, though, I here’s the global wrapper I use:

Most of the code goes inside the $(document).ready() (so lines 4 – 5) or else I write a few functions around line 2 and just call them from inside the ready() function. The main goal is making sure I can use the $ but not cause any conflicts along the way.

In order to throw that into my code quickly and easily, I’ve added a global Snippet to VSCode that allows my to throw it into files quickly and easily. Global snippets are great for chunks of code that you might use regularly and want to inject into your file. This snippet is usually the first thing I do when starting some jQuery in a WordPress project.

Learn Modern WordPress Development

I’m sharing the best resources and tutorials for developers building in and on the block editor.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.