Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
WordPress widget to fetch and display YOURLS shorturl
Name: YOURLS WordPress Widget
Description: A WordPress widget to display a YOURLS shorturl and QR code
Code URI:
Author: Josh Panter
Author URI:
This widget will create &/or fetch the existing short URL for a WordPress post from YOURLS,
provide a button to copy the short url, and optionally display a qrcode.
Install the "Change error messages" plugin
Optional: For QR code display
Install and configure IQRCodes (and U-SRV)
In WordPress:
Install the "Enable Shortcode and PHP in Text widget" plugin and enable PHP evaluation in settings
Create a new text widget with the following code, adjsuted to suit your needs, and restrict visibility to "posts".
Copyright (C) 2018 Josh Panter
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <>.
$api_url = 'https://sho.rt/yourls-api.php';
/* Do not edit below this line */
$url = get_permalink();
$args = array(
'action' => 'shorturl',
'url' => $url,
'format' => 'json',
'signature' => $sig
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
$data = json_decode( $data, true );
$short = $data['shorturl'];
$qr = $data['qrcimg'] ? ' <img style="-webkit-filter: drop-shadow(5px 5px 5px #222); filter: drop-shadow(5px 5px 5px #222); max-width: 128px;" src="'.$data['qrcimg'].'" />' : null;
<div style="text-align: center;">
<div id="short">
<?php echo $short ?>
<button onclick="copyMe()">Copy URL</button>
<?php echo $qr ?>
function copyMe() {
if (document.selection) {
var range = document.body.createTextRange();
alert("Copied: " + range);
} else if (window.getSelection) {
var range = document.createRange();
alert("Copied: " + range);

This comment has been minimized.

Copy link

@alroberts alroberts commented Sep 21, 2021

This seems to keep re-generating new QR codes on the server, causing the cache folder to grow considerably. Also, for some reason, image eventually stops showing (I guess due to 1 min hot linking on SRV-U).. What is the solution for fixing this?

My WordPress site is using cache, and the image is cached too.. but cannot be loaded, as it ends up saying "FAIL: bad access key"a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment