Last active
March 1, 2024 10:06
-
-
Save joshp23/3f990e6ec36e24ba53985968bbfa89f1 to your computer and use it in GitHub Desktop.
WordPress widget to fetch and display YOURLS shorturl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Name: YOURLS WordPress Widget | |
Description: A WordPress widget to display a YOURLS shorturl and QR code | |
Code URI: https://gist.github.com/joshp23/3f990e6ec36e24ba53985968bbfa89f1 | |
Author: Josh Panter | |
Author URI: https://unfettered.net | |
====================================================================== | |
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. | |
In YOURLS: | |
Install the "Change error messages" plugin | |
https://github.com/adigitalife/yourls-change-error-messages | |
Optional: For QR code display | |
Install and configure IQRCodes (and U-SRV) | |
https://github.com/joshp23/YOURLS-IQRCodes | |
https://github.com/joshp23/YOURLS-U-SRV | |
In WordPress: | |
Install the "Enable Shortcode and PHP in Text widget" plugin and enable PHP evaluation in settings | |
https://wordpress.org/plugins/enable-shortcode-and-php-support-in-text-widget/ | |
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 | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
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 <http://www.gnu.org/licenses/>. | |
====================================================================== | |
*/ | |
$api_url = 'https://sho.rt/yourls-api.php'; | |
$sig = 'YOUR_SIGNATURE'; | |
/* 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); | |
curl_close($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 ?> | |
</div> | |
<button onclick="copyMe()">Copy URL</button> | |
<br> | |
<?php echo $qr ?> | |
</div> | |
<script> | |
function copyMe() { | |
if (document.selection) { | |
var range = document.body.createTextRange(); | |
range.moveToElementText(document.getElementById("short")); | |
range.select().createTextRange(); | |
document.execCommand("copy"); | |
alert("Copied: " + range); | |
} else if (window.getSelection) { | |
var range = document.createRange(); | |
range.selectNode(document.getElementById("short")); | |
window.getSelection().addRange(range); | |
document.execCommand("copy"); | |
alert("Copied: " + range); | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.