Generate QR Codes in WordPress Using Google Chart API

A giant QR Code linking to a website, to be re...
Image via Wikipedia

I have already blogged about Google Chart API. You might want to refresh that a bit. In this post let us see a specific use of this API, that is to generate QR Codes. This will be helpful for you, if you are hosting a mobile based

What is a QR Code?

A QR Code is simply a two-dimensional bar-code, readable by QR scanners, mobile phones with a camera, and smartphones.

A QR-Code might look like:
Technophilia - qrcode

You can encode URLs, contact information, SMS, simple text messages and many more into a black-and-white image like the one above.

How To: Manual

You can manually generate QR code here: http://zxing.appspot.com/generator/

There are many mobile QR-Code readers, but I recommend i-nigma. It is recognizes the QR codes instantly and it is not necessary to painstakingly point to the QR-code for a long time. If you don’t have a smartphone, Google also offers a QR Code decoder library: Zebra Crossing (ZXing).

How To: Google Chart API

This is how you can creating these with the Google Chart API:

Simply, there is a new chart type, qr, with attributes to tell the service what to produce:

cht=qr
chl=<text>
choe=<output>

Where <text> is text for the QR code. This must be url-encoded in UTF8.

<output> optionally specifies how the text is encoded into bytes in the QR Code. If this is not specified the default of UTF-8 is used.

How To: Embed in WordPress

WordPress’s get_permalink(); function returns the URL of the current post. (See more options for this function @ WordPress codex.)

Inserting this as a parameter for the chl we can generate QR Code for the current post like:

<img src="http://chart.apis.google.com/chart?cht=qr&chs=150x150&chl=<?php echo get_permalink();?>&chld=H|0">

You can insert this in Single Post (single.php) template of your theme. This template can be edited inside WordPress Admin environment. Goto Appearance->Editor and search for Single Post (single.php) on your right hand-side.

Happy Blogging! :-)