Skip to content

Instantly share code, notes, and snippets.

@picocodes
Created October 7, 2022 04:22
Show Gist options
  • Save picocodes/c1e573169ff6ff15a21f6ca26db271dc to your computer and use it in GitHub Desktop.
Save picocodes/c1e573169ff6ff15a21f6ca26db271dc to your computer and use it in GitHub Desktop.
<?php
add_shortcode( 'gd_qr_code', 'gd_qr_code_shortcode' );
function gd_qr_code_shortcode( $atts ) {
$atts = shortcode_atts(
array(
'url' => '',
'size' => '150',
'alt' => '',
'class' => 'img-responsive',
'style' => '',
),
$atts,
'gd_qr_code'
);
// If no URL is provided, fetch from post meta.
if ( empty( $atts['url'] ) ) {
$atts['url'] = do_shortcode( '[gd_post_meta key="website" show="value-strip" no_wrap="1"]' );
}
// Abort if no URL is provided.
if ( empty( $atts['url'] ) ) {
return '';
}
return printf(
'<img src="https://chart.googleapis.com/chart?chs=%1$sx%1$s&cht=qr&chl=%2$s" alt="%3$s" class="%4$s" style="%5$s">',
absint( $atts['size'] ),
esc_url( $atts['url'] ),
empty( $atts['alt'] ) ? esc_attr( $atts['url'] ) : esc_attr( $atts['alt'] ),
esc_attr( $atts['class'] ),
esc_attr( $atts['style'] )
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment