-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathqr-code-page-link.php
More file actions
67 lines (60 loc) · 2.2 KB
/
qr-code-page-link.php
File metadata and controls
67 lines (60 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* Plugin Name: MU Shortcode: SVG QR for Current URL
* Description: Adds [qr_svg] shortcode that renders an SVG QR code for the current page URL (or a provided URL).
* Author: ChatGPT
* Type: Snippet
* Status: Complete
* Version: 1.0.0
*/
if (!defined('ABSPATH')) exit;
add_shortcode('qr_svg', function($atts = [], $content = null){
$atts = shortcode_atts([
// size in px (square), e.g. 200 => 200x200
'size' => '200',
// ECC error correction level: L, M, Q, H
'ecc' => 'M',
// quiet zone (margin in "modules")
'margin' => '2',
// optional: override URL; otherwise current URL is used
'url' => '',
// optional: CSS class for the <img>
'class' => 'qr-svg',
// optional: alt text
'alt' => 'QR code',
], $atts, 'qr_svg');
// Determine the target URL
$target_url = trim($atts['url']);
if ($target_url === '') {
// Build the exact current URL including scheme, host, and request URI
$scheme = (is_ssl() ? 'https://' : 'http://');
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : parse_url(home_url(), PHP_URL_HOST);
$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
$target_url = $scheme . $host . $uri;
}
// Sanitize/normalize attributes
$size = max(64, (int)$atts['size']); // sensible minimum
$ecc = strtoupper(preg_replace('/[^LMQH]/', 'M', $atts['ecc']));
if (!in_array($ecc, ['L','M','Q','H'], true)) $ecc = 'M';
$margin = max(0, (int)$atts['margin']);
$class = sanitize_html_class($atts['class']);
$alt = esc_attr($atts['alt']);
// Use goqr.me's API (api.qrserver.com) to return SVG directly
// Docs: https://goqr.me/api/
$src = add_query_arg([
'format' => 'svg',
'size' => "{$size}x{$size}",
'ecc' => $ecc,
'margin' => $margin,
'data' => rawurlencode($target_url),
], 'https://api.qrserver.com/v1/create-qr-code/');
// Output <img> pointing to the SVG (browser will render the SVG)
$html = '<img';
$html .= ' src="' . esc_url($src) . '"';
$html .= ' width="' . (int)$size . '" height="' . (int)$size . '"';
$html .= ' loading="lazy" decoding="async"';
if (!empty($class)) $html .= ' class="' . esc_attr($class) . '"';
$html .= ' alt="' . $alt . '"';
$html .= ' />';
return $html;
});