Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**!
- * Validation for general text with no html
- */
- function validate_text_no_html($input) {
- //accept the input only after stripping out all html, extra white space etc!
- return sanitize_text_field($input);
- }
- /**!
- * Validation for general text with some html
- */
- function validate_text_custom_html($input) {
- // accept only a few inline html elements
- $allowed_html = array(
- 'a' => array('href' => array (),'title' => array ()),
- 'b' => array(),
- 'em' => array (),
- 'i' => array (),
- 'strong' => array()
- );
- // trim whitespace
- $valid_input = trim($input);
- // find incorrectly nested or missing closing tags and fix markup
- $valid_input = force_balance_tags($valid_input);
- // only the allowed HTML element names, attribute names and attribute values plus only sane HTML entities will occur
- $valid_input = wp_kses( $valid_input, $allowed_html);
- return $valid_input;
- }
- /**!
- * Accept the input only when numeric
- */
- function validate_text_numeric($input) {
- if ( is_int( $input) || is_float( $input) ) {
- return $input;
- } else {
- return intval( $input);
- }
- }
- /**!
- * Validation for only url
- */
- function validate_text_url($input) {
- // trim whitespace
- $valid_input = trim($input);
- //accept the input only when the url has been sanited for database usage with esc_url_raw()
- $valid_input = esc_url_raw($valid_input);
- return $valid_input;
- }
- /**!
- * Validation for only email
- */
- function validate_text_email($input) {
- //Strips out all characters that are not allowable in an email address.
- $valid_input = sanitize_email($input);
- //accept the input only after the email has been validated
- $valid_input = (is_email($valid_input)!== FALSE) ? $valid_input : '';
- return $valid_input;
- }
- /**!
- * Validation for checkbox
- */
- function validate_checkbox($input) {
- // Our checkbox value is either 0 or 1
- if ( $input == 1 ) {
- return 1;
- } else {
- return 0;
- }
- }
- /**
- * Sanitize a value from a list of allowed values.
- *
- * @param mixed $input The value to sanitize.
- * @param mixed $setting The setting for which the sanitizing is occurring.
- */
- function validate_choice_select_and_radio( $input, $setting ) {
- global $wp_customize;
- $field = $wp_customize->get_control( $setting->id );
- if ( array_key_exists( $input, $field->choices ) ) {
- return $input;
- } else {
- return $setting->default;
- }
- }
- /**!
- * Validation for only inline html
- */
- function validate_textarea_inline_html($input) {
- $valid_input = trim($input); // trim whitespace
- $valid_input = force_balance_tags($valid_input); // find incorrectly nested or missing closing tags and fix markup
- $valid_input = addslashes($valid_input); //calls stripslashes then addslashes
- $valid_input = wp_filter_kses($valid_input); //wp_filter_kses expects content to be escaped!
- $valid_input = stripslashes($valid_input); //calls stripslashes then addslashes
- return $valid_input;
- }
- /**!
- * Validation for no html
- */
- function validate_textarea_no_html($input) {
- // need to add slashes still before sending to the database
- $valid_input = addslashes($input);
- //accept the input only after stripping out all html, extra white space etc!
- $valid_input = wp_filter_nohtml_kses($valid_input);
- $valid_input = stripslashes($valid_input); //calls stripslashes then addslashes
- return $valid_input;
- }
- /**!
- * Validation for allowed line breaks
- */
- function validate_textarea_allow_line_breaks($input) {
- // need to add slashes still before sending to the database
- $valid_input = addslashes($input);
- //accept the input only after stripping out all html, extra white space etc!
- $valid_input = wp_strip_all_tags($valid_input);
- $valid_input = stripslashes($valid_input); //calls stripslashes then addslashes
- return $valid_input;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement