Skip to content

Instantly share code, notes, and snippets.

@scarstens
Created March 21, 2012 19:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scarstens/2151641 to your computer and use it in GitHub Desktop.
Save scarstens/2151641 to your computer and use it in GitHub Desktop.
QR Code Generator - Part of Whitelabel Framework
<?php
//uses the same arguments as WordPress "checked()" function
//but adds the argument submitted and "default"to allow you
//to set the default checked value of the checkbox
function wlwp_checked($checkboxPostedValue, $checkboxDefaultValue = 'on', $echo = false, $requiredField = NULL, $default = false) {
if(empty($requiredField) || (isset($_REQUEST[$requiredField]) && !empty($_REQUEST[$requiredField])) ) {
return checked($checkboxPostedValue, $checkboxDefaultValue, $echo);
}
//if a required field is set, and the required field has not been submitted
//then page is loading for the first time and needs to load default value (whole point of the function)
elseif($default) {
if($echo) echo 'checked="checked"';
return 'checked="checked"';
}
else { global $errors; $errors['wlwp_checked'] = 'wlwp_checked() function failed'; }
}
function qr_code_builder($atts, $content = null) {
if(isset($_REQUEST['qrSubmission']) && !empty($_REQUEST['qrSubmission'])) {
if(!isset($_REQUEST['qrTrackingParam']) || $_REQUEST['qrTrackingParam'] != 'on') $qr_param = '';
elseif(stristr($_REQUEST['qrSubmission'], '?')) $qr_param = '&qr=yes';
else $qr_param = '?qr=yes';
$qr_codes = '<p>'.esc_url($_REQUEST['qrSubmission']).
$qr_param.'<br /><img src="http://api.qrserver.com/v1/create-qr-code/?size=1000x1000&data='.
urlencode(esc_url($_REQUEST['qrSubmission'].$qr_param)).'"></p>';
}
else {
unset($_REQUEST['qrSubmission']);
$qr_codes = '<p><br /><img src="'.get_stylesheet_directory_uri().'/images/myanthem/create-qr-code-sample.png"></p>';
$qr_code_form_defaults['tracking'] = 'checked="checked"';
}
$qr_code_form_EOL = '<br />';
$qr_code_form_EOL = apply_filters('wlwp_qr_code_form_line_break', $qr_code_form_EOL);
$qr_code_form =
'<form id="qr-code-form" method="post">'.
'<label for="qrSubmission"> URL: <input type="text" id="qrSubmission" name="qrSubmission" value="'.$_REQUEST['qrSubmission'].'" /></label>'.$qr_code_form_EOL.
'<label for="qrTrackingParam">(default checked) Tracking Parameter: <input type="checkbox" '.checked($_REQUEST['qrOptionsTrackingA'], 'on', false).' id="qrTrackingParam" name="qrOptionsTrackingA" /></label>'.$qr_code_form_EOL.
'<label for="qrTrackingParam">(custom checked) Tracking Parameter: <input type="checkbox" '.wlwp_checked($_REQUEST['qrOptionsTrackingB'], 'on', false).' id="qrTrackingParam" name="qrOptionsTrackingB" /></label>'.$qr_code_form_EOL.
'<label for="qrTrackingParam">(custom checked->on) Tracking Parameter: <input type="checkbox" '.wlwp_checked($_REQUEST['qrOptionsTrackingC'], 'on', false, 'qrSubmission', true).' id="qrTrackingParam" name="qrOptionsTrackingC" /></label>'.$qr_code_form_EOL.
'<input type="submit" value="Build QR Code" />'.
'</form>';
return $qr_code_form.$qr_codes;
}
add_shortcode("qr-code-builder", "qr_code_builder");
?>
@norcross
Copy link

have you confirmed that the $_REQUEST['qrOptionsTracking'] variable is available when this code loads?

@scarstens
Copy link
Author

I have not, but the code is still a work in progress. mostly I don't see a way to make the checkbox be "checked" by default using the wordpress function. although now I've determined adding more code is the only option which is where I'm heading with line 13.

@norcross
Copy link

I'm using the function in a plugin I'm building now. 51 instances of it, to be exact :) the other thing I'd do (once you've confirmed that variable is available) is set it as it's own variable, then call THAT in the function

$tracking = $_REQUEST['qrOptionsTracking'];
checked($tracking, 'on', false)

@scarstens
Copy link
Author

Yes, I see that, but the form still defaults to that checkbox being "unchecked" before the form is submitted. I want the checkbox to be checked by default... no options the in the function for that ::tear::

@scarstens
Copy link
Author

just built my own extended function, to allow "default value" for the checkbox, the shortcode now spits out 3 tests of the same checkbox for testing, 1 using the old checked() function, 1 using the new "wlwp_checked" function with the exact same parameters, and the final example to show the new "default checked value".

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