Advertisement
sayful

WP Data Validation

May 3rd, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.82 KB | None | 0 0
  1. <?php
  2. /**!
  3.  * Validation for general text with no html
  4.  */
  5. function validate_text_no_html($input) {
  6.     //accept the input only after stripping out all html, extra white space etc!
  7.     return sanitize_text_field($input);
  8. }
  9. /**!
  10.  * Validation for general text with some html
  11.  */
  12. function validate_text_custom_html($input) {
  13.     // accept only a few inline html elements
  14.     $allowed_html = array(
  15.         'a' => array('href' => array (),'title' => array ()),
  16.         'b' => array(),
  17.         'em' => array (),
  18.         'i' => array (),
  19.         'strong' => array()
  20.     );
  21.    
  22.     // trim whitespace                 
  23.     $valid_input    = trim($input);
  24.     // find incorrectly nested or missing closing tags and fix markup
  25.     $valid_input    = force_balance_tags($valid_input);
  26.     // only the allowed HTML element names, attribute names and attribute values plus only sane HTML entities will occur
  27.     $valid_input    = wp_kses( $valid_input, $allowed_html);
  28.  
  29.     return $valid_input;
  30. }
  31.  
  32. /**!
  33.  * Accept the input only when numeric
  34.  */
  35. function validate_text_numeric($input) {
  36.  
  37.     if ( is_int( $input) || is_float( $input) ) {
  38.  
  39.         return $input;
  40.  
  41.     } else {
  42.  
  43.         return intval( $input);
  44.  
  45.     }
  46. }
  47.  
  48. /**!
  49.  * Validation for only url
  50.  */
  51. function validate_text_url($input) {
  52.     // trim whitespace
  53.     $valid_input    = trim($input);
  54.     //accept the input only when the url has been sanited for database usage with esc_url_raw()
  55.     $valid_input    = esc_url_raw($valid_input);
  56.      
  57.     return $valid_input;
  58. }
  59.  
  60. /**!
  61.  * Validation for only email
  62.  */
  63. function validate_text_email($input) {
  64.     //Strips out all characters that are not allowable in an email address.
  65.     $valid_input    = sanitize_email($input);
  66.     //accept the input only after the email has been validated
  67.     $valid_input    = (is_email($valid_input)!== FALSE) ? $valid_input : '';
  68.      
  69.     return $valid_input;
  70. }
  71.  
  72. /**!
  73.  * Validation for checkbox
  74.  */
  75. function validate_checkbox($input) {
  76.     // Our checkbox value is either 0 or 1
  77.     if ( $input == 1 ) {
  78.  
  79.         return 1;
  80.  
  81.     } else {
  82.  
  83.         return 0;
  84.        
  85.     }
  86. }
  87.  
  88. /**
  89.  * Sanitize a value from a list of allowed values.
  90.  *
  91.  * @param  mixed    $input      The value to sanitize.
  92.  * @param  mixed    $setting    The setting for which the sanitizing is occurring.
  93.  */
  94. function validate_choice_select_and_radio( $input, $setting ) {
  95.  
  96.     global $wp_customize;
  97.     $field = $wp_customize->get_control( $setting->id );
  98.  
  99.     if ( array_key_exists( $input, $field->choices ) ) {
  100.  
  101.         return $input;
  102.  
  103.     } else {
  104.  
  105.         return $setting->default;
  106.  
  107.     }
  108. }
  109.  
  110. /**!
  111.  * Validation for only inline html
  112.  */
  113. function validate_textarea_inline_html($input) {
  114.     $valid_input    = trim($input);     // trim whitespace
  115.     $valid_input    = force_balance_tags($valid_input); // find incorrectly nested or missing closing tags and fix markup
  116.     $valid_input    = addslashes($valid_input); //calls stripslashes then addslashes
  117.     $valid_input    = wp_filter_kses($valid_input); //wp_filter_kses expects content to be escaped!
  118.     $valid_input    = stripslashes($valid_input);   //calls stripslashes then addslashes
  119.      
  120.     return $valid_input;
  121. }
  122.  
  123. /**!
  124.  * Validation for no html
  125.  */
  126. function validate_textarea_no_html($input) {
  127.     // need to add slashes still before sending to the database
  128.     $valid_input    = addslashes($input);
  129.     //accept the input only after stripping out all html, extra white space etc!
  130.     $valid_input    = wp_filter_nohtml_kses($valid_input);
  131.     $valid_input    = stripslashes($valid_input);   //calls stripslashes then addslashes
  132.      
  133.     return $valid_input;
  134. }
  135.  
  136. /**!
  137.  * Validation for allowed line breaks
  138.  */
  139. function validate_textarea_allow_line_breaks($input) {
  140.     // need to add slashes still before sending to the database
  141.     $valid_input    = addslashes($input);
  142.     //accept the input only after stripping out all html, extra white space etc!
  143.     $valid_input    = wp_strip_all_tags($valid_input);
  144.     $valid_input    = stripslashes($valid_input);   //calls stripslashes then addslashes
  145.      
  146.     return $valid_input;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement