Generate QR Codes in WordPress Using a Shortcode

In an increasingly mobile-centric world, QR codes have made it incredibly easy to bridge the gap between offline and online content. Whether you’re running an event, managing a restaurant, or writing a blog, having a QR code that leads to your website can be incredibly convenient. In this guide, we’ll walk through creating a WordPress shortcode that dynamically generates a QR code for the current page’s URL.

Why Use a QR Code?

QR codes are versatile tools for marketing, sharing information, and creating easy access points to your website’s content. They can be scanned using a smartphone camera, directing users to a specific URL without the need for typing.


Before you start, ensure that your WordPress site has a Code Snippets plugin installed, as this method prevents potential issues arising from editing the theme’s functions.php file.

Step 1: Install the QR Code Library

To generate QR codes, we’ll use a PHP library like endroid/qr-code. Install this library via Composer in your WordPress installation. If you’re not familiar with Composer or prefer not to use it, look for an alternative QR code library that can be included directly into your theme.

Step 2: Create the Shortcode Function

Create a new snippet with the following PHP code to generate the QR code:

function current_page_qr_code() { // Include the QR Code library (adjust the path as needed if not using Composer) require_once 'vendor/autoload.php'; // Get the current page URL $current_url = get_permalink(); // Generate the QR code $qrCode = new \Endroid\QrCode\QrCode($current_url); $qrCode->setSize(300); // Set the size of the QR code // Output the QR code as a base64 image $qrCodeDataUri = $qrCode->writeDataUri(); // Return an image element with the QR code data URI as the source return '<img src="' . esc_attr($qrCodeDataUri) . '" alt="QR Code for this page">'; } add_shortcode('current_page_qr', 'current_page_qr_code');

Step 3: Add the Shortcode

With the shortcode function created, you can now add the [current_page_qr] shortcode to any page or post where you want to display the QR code for that URL.

Step 4: Styling the QR Code

Optionally, you can add custom CSS to style the QR code’s appearance on your page:

img.qr-code { display: block; margin: 0 auto; // Center the QR code image max-width: 100%; // Make sure it's responsive height: auto; }

Remember to add the qr-code class to the img tag in the shortcode function if you want to use the above CSS.


Adding a QR code to your WordPress pages can be a clever way to engage users and provide quick access to your content. By using a shortcode, you simplify the process of including a dynamic QR code that reflects the current page’s URL. This method ensures that your QR codes are always up-to-date without any manual intervention.

Incorporate this feature into your WordPress site and enjoy the seamless integration of offline and online user experiences!