WordPress

Create QR Codes For Your WordPress Posts

QR can be used by mobile devices to scan and get bits of data, they can even be used to display a web page.

They are starting to become more and more popular the more mobile devices are being used.

But creating your own QR codes can be difficult and if you want one for each post of your blog it can be very time consuming. There is a company called QR-Server which provide an API to create QR codes, all you have to do is query this API with the width and height of the QR code you want, the website you want it to redirect to and it will generate a QR code for you.

http://api.qrserver.com/v1/create-qr-code/?size=150x150&data=url-or-text

Paulund QR Code

WordPress Shortcode

Using this API we can create a QR code for any page, now we are going a WordPress shortcode to generate the QR of the current page.

Just copy and paste the following into your functions.php page to create the shortcode.

function currentpage_qr_code( $atts ) {
    extract(shortcode_atts(array(
        'size' => '300'
    ), $atts)); 

    global $post;  

    return '<img src="http://api.qrserver.com/v1/create-qr-code/?size='.$size.'x'.$size.'&amp;data='.get_permalink( $post->ID ).'" alt="'.$post->post_title.'"/>';  

}
add_shortcode('currentqr', 'currentpage_qr_code');

Add the shortcode anywhere in your posts to generate a QR code for the current page.

[currentqr]

Create QR Codes For Your WordPress Posts

All you have to do is scan this QR with your mobile and you will be able to see this page on your phone.

Back to top

Learn how to code with Treehouse

  • Learn projects with access to 1000+ videos
  • Practice live with our Code Challenge Engine
  • Get help in our members-only forums

Start with a 7 day free trial