Skip to content

Instantly share code, notes, and snippets.

@mattmcgiv
Created October 29, 2015 17:46
Show Gist options
  • Select an option

  • Save mattmcgiv/87df8dec627d0624bbd2 to your computer and use it in GitHub Desktop.

Select an option

Save mattmcgiv/87df8dec627d0624bbd2 to your computer and use it in GitHub Desktop.
Some useful functions for sanitization, validation, and escaping output
<?php
function mm_sanitize($unsanitized_input) {
//calls a WordPress core function
//verifies UTF-8 encoding, strips tags
//removes whitespace/newlines & octets
//reference:
//https://codex.wordpress.org/Function_Reference/sanitize_text_field
return sanitize_text_field($unsanitized_input);
}
function mm_float_validate($unvalidated_input) {
//Returns the numerical float value of the input
//junk strings and arrays return 0.0
//reference: http://php.net/manual/en/function.floatval.php
return floatval($unvalidated_input);
}
function mm_esc_js($js_val) {
//escapes the newline & other unexpected tokens
//prior to outputting them on the page
//JSON_NUMERIC_CHECK option converts strings to
//their numeric equivalents
//reference: http://php.net/manual/en/function.json-encode.php
return json_encode($js_val, JSON_NUMERIC_CHECK);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment