my alt

Creating a WordPress Plugin to Generate QR Codes

This tutorial provides a step-by-step guide on creating a WordPress plugin that generates QR codes linking to your post URL. Using the phpqrcode library, you can accomplish this task without the need for a composer. All you need is a text file editor or a code editor, and about 30 minutes of your time.

Step 1: Create the Plugin Folder and Files

Start by creating a new folder. We’ll call this new folder tkt-qr-code. In this folder, create a new PHP file and name it tkt-qr-code.php. This will be our plugin’s main file.

Step 2: Download and Include the phpqrcode Library

For generating QR codes, we are using the PHP QR Code library. Download it from its GitHub repository found at https://github.com/Darkflib/php-qrcode (or from sourceforge). Unzip the downloaded file and rename the extracted folder to phpqrcode. Now, add this folder into the tkt-qr-code plugin folder.

Hint: Once you run this plugin on a WordPress install, make sure the GD2 PHP extension is installed and enabled on your server. It is required by the phpqrcode library. You can check this from the phpinfo() or by asking your hosting provider.

Step 3: Write the Plugin’s Main File

In the tkt-qr-code.php file, we’ll add the following code right after the opening <?php tag

<?php

/**
 * Plugin Name: TKT QR Code
 * Plugin URI: https://www.tukutoi.com
 * Description: This plugin generates QR codes
 * Version: 1.0
 * Author: TukuToi Co Ltd
 * Author URI: https://www.tukutoi.com
 * License: GPL3
 */ require_once plugin_dir_path( __FILE__ ) . 'phpqrcode/qrlib.php'; 

The comments at the top of the file provide WordPress with the information it needs to handle your plugin correctly. The require_once line is to include the phpqrcode library into our plugin.

Step 4: Add the QR Code Generating Function

Next, we will write a function named qr_code_func to generate the QR code using a ShortCode. The ShortCode will accept two parameters – size and post_id. If not provided, it will default to ‘150’ and the current post ID respectively. The function will return an HTML image tag with the URL of the generated QR code.


function qr_code_func( $atts ) {
	// Extract shortcode attributes.
	$atts = shortcode_atts(
		array(
			'size'    => '150', // Default size.
			'post_id' => get_the_ID(), // Default post ID.
		),
		$atts,
		'qr_code' // Shortcode tag.
	);

	$post_id = absint( $atts['post_id'] );
	$size    = absint( $atts['size'] );

	$url = esc_url_raw( trailingslashit( get_site_url() ) . '?p=' . $post_id );

	$qr_dir   = plugin_dir_path( __FILE__ ) . '/qrcodes/';
	$filename = $qr_dir . 'post_' . $post_id . '.png';

	// Check if the file exists.
	if ( ! file_exists( $filename ) ) {
		// Create the QR code.
		QRcode::png( $url, $filename, 'L', 10, 2 );
	}

	// Return the image tag.
	$qr_url = esc_url_raw( plugin_dir_url( __FILE__ ) . '/qrcodes/post_' . $post_id . '.png' );
	return '<img src="' . $qr_url . '" height="' . $size . '" width="' . $size . '">';
}
add_shortcode( 'qr_code', 'qr_code_func' );

This function will use the PHP QR Code library to create a QR code with the given size and the URL of the specified post ID. If the file for the QR code already exists, it won’t be regenerated. The QR code images will be saved in the “qrcodes” subdirectory of our plugin folder.

Step 5: Activate the Plugin

Now, Zip up the entire folder tkt-qr-code, go to the WordPress admin dashboard, navigate to “Plugins” > “Add New”, and upload your new plugin. Then, click “Activate”, and voila! Your plugin is now activated.

Step 6: Use the Shortcode

You can now use the shortcode [qr_code] in your posts or pages to display the QR code. If you want to specify the size or the post ID, you can do so by using the shortcode like this: [qr_code size="200" post_id="123"], where “200” is the size and “123” is the post ID.

Conclusion

That’s it! You have successfully created a WordPress plugin that generates QR codes linking to your post URL. Feel free to modify the code to suit your needs and remember, the best way to learn is by doing. However, you can also download the finished plugin in the TukuToi QR Code WordPress Plugin GitHub Repository, if you simply want to test it out or use it at your discretion.