Using Javascript in WordPress Themes

If you’re planning to use javascript on your WordPress blog or website, there is one function with which you should become intimately familiar. That function is wp_enqueue_script(). It will also be helpful if you do a little bit of research into the wp_deregister_script() function, though, the only official information you’ll find about that function is in the codex information about the wp_enqueue_script() function.

Basically, this function keeps a log of all of the javascript files and libraries you want to use in your theme, makes sure you aren’t duplicating any, and then outputs them in the right order. I would recommend setting the $in_footer parameter to boolean true for all of the scripts you enqueue, as that causes WordPress to add the javascript calls to the footer of your pages rather than putting them in the header. In order to do this, though, you need to make sure that you include the wp_footer() function inside of your theme (preferably just above the closing </body> tag).

Determine Current Page in WordPress

Lately, I’ve been doing a lot of work with WordPress themes. On one particular site, the navigation menu is built dynamically, combining pages and categories in the navigation scheme. I needed to find a way to determine which page or category was currently being viewed so I could apply a special class to the item in the navigation menu, indicating that the link is active.

After a bit of research, I found a couple of useful WordPress functions that helped me determine which page the visitor is currently viewing.

Some Handy Array Functions in PHP

This post will go over the basics of some very nice array-related functions that are built in to PHP. If you use arrays fairly often in your PHP code, you will no doubt find a use for each of these handy functions over time.