rest_sanitize_value_from_schema

Sanitize a value based on a schema.

Description

(true|WP_Error) rest_sanitize_value_from_schema( (mixed) $value, (array) $args ); 

Returns (true|WP_Error)

Parameters (2)

0. $value (mixed)
The value to sanitize.
1. $args (array)
Schema array to use for sanitization.

Usage

  1. if ( !function_exists( 'rest_sanitize_value_from_schema' ) ) { 
  2. require_once ABSPATH . WPINC . '/rest-api.php'; 
  3.  
  4. // The value to sanitize. 
  5. $value = null; 
  6.  
  7. // Schema array to use for sanitization. 
  8. $args = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = rest_sanitize_value_from_schema($value, $args); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/rest-api.php  
  1. function rest_sanitize_value_from_schema( $value, $args ) { 
  2. if ( 'array' === $args['type'] ) { 
  3. if ( empty( $args['items'] ) ) { 
  4. return (array) $value; 
  5. if ( ! is_array( $value ) ) { 
  6. $value = preg_split( '/[\s, ]+/', $value ); 
  7. foreach ( $value as $index => $v ) { 
  8. $value[ $index ] = rest_sanitize_value_from_schema( $v, $args['items'] ); 
  9. // Normalize to numeric array so nothing unexpected 
  10. // is in the keys. 
  11. $value = array_values( $value ); 
  12. return $value; 
  13. if ( 'integer' === $args['type'] ) { 
  14. return (int) $value; 
  15.  
  16. if ( 'number' === $args['type'] ) { 
  17. return (float) $value; 
  18.  
  19. if ( 'boolean' === $args['type'] ) { 
  20. return rest_sanitize_boolean( $value ); 
  21.  
  22. if ( isset( $args['format'] ) ) { 
  23. switch ( $args['format'] ) { 
  24. case 'date-time' : 
  25. return sanitize_text_field( $value ); 
  26.  
  27. case 'email' : 
  28. /** 
  29. * sanitize_email() validates, which would be unexpected. 
  30. */ 
  31. return sanitize_text_field( $value ); 
  32.  
  33. case 'uri' : 
  34. return esc_url_raw( $value ); 
  35.  
  36. case 'ip' : 
  37. return sanitize_text_field( $value ); 
  38.  
  39. if ( 'string' === $args['type'] ) { 
  40. return strval( $value ); 
  41.  
  42. return $value;