Using jQuery and Javascript in WordPress Templates

There are a ton of tutorials out there about how to include javascript and jQuery in WordPress. I probably read most of them as I was adapting my WordPress based CMS to use my newly gained jQuery skills. The theme writer’s approach to including javascript is to modify your theme’s functions.php file and call the wp_enqueue_script function to trigger when wp_print_scripts action fires. However, for the tweak happy folks like me, there’s actually an easier way that I stumbled upon reading the excellent wpengineer blog.

I was reading about contact forms at wpengineer and the code included the wp_enqueue_script call right in the template before the get_header() call. Brilliant! I had never read that about that technique. You don’t have to write conditional statements nor hook in within the functions.php file. This is especially handy if you use a premium theme. Premium themes usually have a functions.php file that includes a huge caveat not to modify it (for a lot of good reasons). An even better benefit to modifying the template is as your theme is updated, you don’t have to worry about carrying over custom edits.

In Praise of jQuery

I finally got around to jumping into javascript, specifically jQuery. It’s an awesome framework and a valuable part of the toolbox. Now I haven’t really found anything I couldn’t do server-side in php but there are certainly a bunch of things that are just easier using client-side, especially styling (which I hate). Using jQuery has improved my html coding, especially the use of optional attributes, which jQuery exploits quite nicely. There’s more to css life than just styles and ids.

When I first started with jQuery, all I saw was simple hide/shows and effects. It really didn’t take long to figure out how to use AJAX which not only handles form submissions but it lets me take a chunk of output and use it all over my static site. Change it once and it’s updated all over.

There are a lot of plugins around for jQuery to handle a lot of routine tasks like form validation and form submission. While I started out using plugins, I’ve eventually figured out it’s just as easy to code a couple of lines and have a customized solution that you can understand and manipulate.

Javascript and jQuery have a learning curve like most everything else but it’s worthwhile to give it a try.

jQuery: I want to use it but…

It’s taken me the better part of a year to teach myself how to code in php and write MySql queries. Now I’m on to javascript and it’s not going well. Actually, I’m not really working that hard at javascript but I am interested in jQuery since it appears to be perfect for someone with a dangerously limited knowledge of javascript.

I haven’t gotten very far with jQuery. Probably because I don’t understand javascript. I wrote a couple of pages which use hide/show and animated transitions but that’s it. Just glitz rather than heavy lifting. I know I can use jQuery’s AJAX functions to call remote scripts via POST but I want to do something with the data that comes back. I just haven’t figured out how and I can’t seem to find a good online tutorial for this. Oh well.

jQuery: The Write Less, Do More, JavaScript Library