Javascript and WordPress – The Definitive Guide

Using custom JavaScript code in a WordPress theme or plugin is, in many cases, a given. Fortunately, WordPress comes bundled with a selection of popular Javascript libraries (jQuery, Prototype and others) for use with your plugins and themes. Many users, however, simply write the `<script>` tags in the header.php file of their theme or as part of a function in their plugin that is run in the header of the theme being used. This is a potential problem area that can have you, the developer, sitting for ages looking at your code and wondering why plugin `X` isn’t working correctly when theme `Y` is active. This guide aims to provide an understanding of how to correctly enqueue Javascript in WordPress and how to avoid potential Javascript conflicts.

Okay, what are we doing here?

We’re going to enqueue the Javascript files, used by our WordPress plugin or theme, using the correct method and the wp_enqueue_script()function. We will be adding an action to the wp_print_scripts() action and, inside a function within our theme or plugin, running the wp_enqueue_script() function. We will also be including the Javascript in the administration area only, creating dependencies between our various custom Javascript files and enqueuing the scripts on only a specific page in the administration area. Continue reading

A few guidelines for WordPress plugin development

The WordPress plugin API is vast and powerful. It allows developers to essentially hook code into almost any area of the WordPress system without modifying the core files at all. It also allows for the creation of standalone plugins that work within the WordPress system but do not hook into the core modules.

Over the last few weeks, WordPress plugin development has become one of my favourite things to do. I find it exciting to be able to create functionality, incorporate it seemlessly into the WordPress system and see it work smoothly with the other modules. While plugin development for WordPress is incredibly powerful, it also carries with it a few areas where people commonly stumble over and potentially lose interest in their code… which could be the next big thing. Here are a few guidelines I’ve picked up in order to step over the stumbling blocks.
Continue reading

Getting the information of a root page in WordPress

Often, when using WordPress as a content management or blogging tool on a website, one uses a multi-level page structure. This entails a page being listed “underneath” another page in a tree structure. Usually, if your WordPress installation’s permalinks deviate from the default setting, your page URL structure will look something like this:

http://websiteurl.com/parentpageslug/subpageslug (Note: note a real URL)

What if you require a value to be set depending on the highest level parent page? The process looks like this:

Continue reading

WordPress Shortcodes… in short

In the spirit of the topic, I’ll keep this post short and sweet. Today I’ll be discussing WordPress’s shortcode API and how to utilise it.

WordPress shortcodes are codes (eg: [list_bookmarks]) which can be typed into the content area of a page or post in order to provide functionality of some kind. Shortcodes can be wrapped around text (similarly to how HTML tags are wrapped around text) or can be used as a single tag. In this post, I will discuss using shortcodes as a single tag to save time as well as providing more control to the user of your WordPress theme/plugin/function.
Continue reading

HOW TO: Use your mobile phone as a modem for your Mac

I’ve wanted to try this out for a while now, and it works surprisingly well. In fact, I’m using it to type this post. Anyways, onto the ‘how-to’.

What you’ll need is a laptop or desktop Mac (I’m on a Macbook) and a mobile phone that supports bluetooth and a 3G connection of sorts (any kind of data transfer connection should work fine). Let me first lay out what we’re aiming to do here, step by step. We want to connect our laptop or desktop to the phone via bluetooth, and to use the phone’s data connection as a means of accessing the web. Here’s how we do this:
Continue reading

HOW TO: Make a refreshing sorbet

OK, so summer is definitely coming on strong at the moment. With temperatures in the Cape reaching highs of 34° and up (also showing no signs of cooling down too much), keeping cool and refreshed is becoming more and more difficult. A few months ago, I blogged about websites for “web 2.0 chefs“.  During the research for this post, I discovered some awesome websites that I have made use of since then. The recipe I used for reference for this sorbet (thanks must go to Jamie Oliver) was found using Cookstr, a great resource for finding recipes to suit your exact requirements (budget, meal, season, etc).

In the incredible heat over this weekend past, I created a basic sorbet (leaving one or two flavours out of the recipe linked above). I found it to be very refreshing. This is how I did it:

You will need:

1 cup of water.
¾ cup of sugar (I used brown. It changes the colour slightly but is possibly healthier than white).
Juice and zest of 2-3 lemons (use as many as you like. It depends how sour you like it. 2-3 compliments the sugar nicely.)

OK, this is how you do it:

1. Bring the water and sugar to a boil on the stove, making sure to stir and disolve the sugar. Once boiling, simmer for 5 minutes.
2. Once simmered, remove from the stove and allow to cool for a bit (use this time to get your lemons ready).
3. Once cooled, mix in the lemon juice and zest.
4. Pour into a tupperware container and store in the freezer.

This should take around 2 hours to set. I left mine in for a bit longer. Check up on it every so often and judge when you feel it’s ready.

I’ve since tried this recipe with berry juice to replace the lemons. It may end up being a tad sweet. I’m keen to check it out though. When the weather just isn’t calming down, here’s another way to get refreshed and cool down a bit.

Image Courtesy: Cookstr.

Do you have a sorbet recipe? How did the above recipe work out for you? Share your thoughts in the comments. 

HOW TO: Set up a rockstar video blog

Video blogging (‘vlogging’, for short) is revolutionising the way internet users access information and interact with one another. Watching a 5 minute video clip or tutorial is, for many, easier than reading an article that could easily span two or three printed pages. The average internet user can now, with a few clicks and an original idea, become an online, world-famous, video rockstar. If this is your one true dream (being a rockstar other than your Guitar Hero skills while the dog watches you with it’s head tilted slightly to the left), here are a few basic steps to rock stardome…and setting up a video blog (no instruments or years of musical training required). Continue reading

HOW TO: Track the sports score using Twitter

Here’s an interesting paradox. Sports (lively, active, outdoor, sweaty) and Twitter (prodominantly indoor, digital, non-verbal, ‘geeky’). Who ever thought that ‘geeks’ liked sport? It seems that many do.

Since the cricket started a few weeks ago, I’ve seen streams of tweets coming in from avid cricket fans, commentating on virtually every play and sharing their thoughts. This is a great way to get the score and the latest info on what’s going on with the game currently being played. Just the other day (before, during and after the Manchester United vs Chelsea soccer game) I saw a tweet congratulating Manchester United and, almost straight after in the same stream, a tweet from another user cheering on Chelsea. What is the common denominator here?
Continue reading